php laravel根据月份顺序中的月份名称对项目进行排序数组

php laravel根据月份顺序中的月份名称对项目进行排序数组,php,laravel,sorting,collections,eloquent,Php,Laravel,Sorting,Collections,Eloquent,我有一个结果集,比如 {"data_points": [{ "monthname": "Apr", "value": 62 },{ "monthname": "Aug", "value": 1904.53 },{ "monthname": "Feb", "value": 33.33 },{ "monthname": "Jan", "value": 2033.33 },{ "monthname": "Jul", "va

我有一个结果集,比如

{"data_points":
[{
    "monthname": "Apr",
    "value": 62
},{
    "monthname": "Aug",
    "value": 1904.53
},{
    "monthname": "Feb",
    "value": 33.33
},{
    "monthname": "Jan",
    "value": 2033.33
},{
    "monthname": "Jul",
    "value": 90.83
},{
    "monthname": "Dec",
    "value": 2030
},{
    "monthname": "Mar",
    "value": 100
},{
    "monthname": "Sep",
    "value": 433.33
},{
    "monthname": "May",
    "value": 5670.93
},{
    "monthname": "Jun",
    "value": 40.4
},{
    "monthname": "Oct",
    "value": 0
},{
    "monthname": "Nov",
    "value": 31
}]}
我想按数据点对项目进行排序。monthname使用月份顺序,如

{"data_points": [{"monthname":"Jan", "value": 2033.33}, {"monthname": "Feb", "value":33.33} ........]}

更新

为了使用Laravel提供的方法对集合进行排序,可以使用以下代码

$collection->sort(function ($a, $b) {
            $monthA = date_parse($a['monthname']);
            $monthB = date_parse($b['monthname']);

            return $monthA["month"] - $monthB["month"];
        });
Laravel文档提到,如果排序很复杂,可以使用回调

下面是您想要在普通PHP中排序的代码

$data = '{"data_points": [{ "monthname": "Apr", "value": 62 },{ "monthname": "Aug", "value": 1904.53 },{ "monthname": "Feb", "value": 33.33 },{ "monthname": "Jan", "value": 2033.33 },{ "monthname": "Jul", "value": 90.83 },{ "monthname": "Dec", "value": 2030 },{ "monthname": "Mar", "value": 100 },{ "monthname": "Sep", "value": 433.33 },{ "monthname": "May", "value": 5670.93 },{ "monthname": "Jun", "value": 40.4 },{ "monthname": "Oct", "value": 0 },{ "monthname": "Nov", "value": 31 }]}';
$decoded =  json_decode($data, true);

usort($decoded['data_points'], "compare_months");

$data = json_encode( $decoded );

print_r($data);

function compare_months($a, $b) {
    $monthA = date_parse($a['monthname']);
    $monthB = date_parse($b['monthname']);

    return $monthA["month"] - $monthB["month"];
}
基本上,您需要将月份转换为数值。然后根据这一点进行排序


以下是更新的工作演示

为了使用Laravel提供的方法对集合进行排序,可以使用以下代码

$collection->sort(function ($a, $b) {
            $monthA = date_parse($a['monthname']);
            $monthB = date_parse($b['monthname']);

            return $monthA["month"] - $monthB["month"];
        });
Laravel文档提到,如果排序很复杂,可以使用回调

下面是您想要在普通PHP中排序的代码

$data = '{"data_points": [{ "monthname": "Apr", "value": 62 },{ "monthname": "Aug", "value": 1904.53 },{ "monthname": "Feb", "value": 33.33 },{ "monthname": "Jan", "value": 2033.33 },{ "monthname": "Jul", "value": 90.83 },{ "monthname": "Dec", "value": 2030 },{ "monthname": "Mar", "value": 100 },{ "monthname": "Sep", "value": 433.33 },{ "monthname": "May", "value": 5670.93 },{ "monthname": "Jun", "value": 40.4 },{ "monthname": "Oct", "value": 0 },{ "monthname": "Nov", "value": 31 }]}';
$decoded =  json_decode($data, true);

usort($decoded['data_points'], "compare_months");

$data = json_encode( $decoded );

print_r($data);

function compare_months($a, $b) {
    $monthA = date_parse($a['monthname']);
    $monthB = date_parse($b['monthname']);

    return $monthA["month"] - $monthB["month"];
}
基本上,您需要将月份转换为数值。然后根据这一点进行排序


下面是关于

的工作演示,如果它是一个集合,为什么不使用集合方法

范例

$sorted = $collection->sortBy('month');

我认为它在一个月内不起作用,但在这种情况下,您可以使用
map
每个
方法遍历集合,并另存为另一个集合或数组。

如果它是一个集合,为什么不使用集合方法

范例

$sorted = $collection->sortBy('month');

我认为它在一个月内不起作用,但在这种情况下,您可以使用
map
每个
方法遍历集合,并另存为另一个集合或数组。

在答案的帮助下,我得到了一个最佳解决方案

$sort_collect = $result->sortBy(function ($item, $i) { 
    return ( Carbon::parse($item->monthname)->format("m") );
});
return $sort_collect;
返回所需的顺序,
谢谢两位@ascsoftw,Prafulla Kumar Sahu

在答案的帮助下,我找到了一个最佳解决方案

$sort_collect = $result->sortBy(function ($item, $i) { 
    return ( Carbon::parse($item->monthname)->format("m") );
});
return $sort_collect;
返回所需的顺序,

谢谢两位@ascsoftw,Prafulla Kumar Sahu

你确定,这是一个数组还是一个集合?是的,结果集是一个集合?是的,结果集是一个集合。我期待使用laravel雄辩的集合解决方案。你必须在laravel中使用PHP来重新格式化数组。不,我们正在处理laravel集合,不期望转换为数组的开销。@ShivarajRH我已经更新了按laravel集合排序的答案。我觉得使用date_parse()有点慢,有什么最佳方法吗?我期望使用laravel eloquent Collection的解决方案您必须在laravel中使用PHP来重新格式化数组。没有,我们正在处理laravel集合,不期望转换为数组的开销。@ShivarajRH我已经更新了按laravel集合排序的答案。我觉得使用date_parse()有点慢,有什么最佳方法吗?没有,这将按字母顺序按月份名称排序,但不是按默认月份顺序@ShivarajRH然后遍历该集合并保存在另一个集合或数组中,或者更好。当您从数据库中检索时,使用sort by。抱歉,兄弟,这是一个雄辩的集合,它包含大量数据,laravel person会理解我共享的格式。@ShivarajRH那么为什么在检索之后而不是在从数据库检索时尝试排序?不,这将按月份名称按字母顺序排序,但不会按默认月份顺序排序@ShivarajRH然后遍历该集合并保存在另一个集合或数组中,或者更好。当您从数据库中检索时,使用sort by。抱歉,兄弟,这是一个雄辩的集合,它包含大量数据,laravel person会理解我共享的格式。@ShivarajRH那么为什么在检索之后而不是从数据库检索时尝试排序?