【问题标题】:php laravel sort array of items based on month name in month orderphp laravel 根据月份名称按月份顺序对项目数组进行排序
【发布时间】:2019-09-06 06:04:13
【问题描述】:

我有一个类似的结果集,

{"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
}]}

我想使用月份顺序按 data_points.monthname 对项目进行排序,例如

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

【问题讨论】:

  • 你确定是数组还是集合?
  • 是的,结果集是一个集合

标签: php laravel sorting collections eloquent


【解决方案1】:

如果是集合,为什么不使用sortBy集合方法。

例子

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

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

【讨论】:

  • 不,这将按月份名称按字母顺序排序,但不是按默认月份顺序!
  • @ShivarajRH 然后遍历集合并保存在另一个集合或数组中,或者当您从数据库中检索时更好,使用 sort by there。
  • 对不起兄弟,这是一个 Eloquent 集合,它的海量数据,laravel 的人会理解我分享的格式。
  • @ShivarajRH 那么为什么在检索后而不是在从数据库检索时尝试排序?
【解决方案2】:

更新

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

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

            return $monthA["month"] - $monthB["month"];
        });

Laravel 文档提到,如果您的排序很复杂,您可以使用回调到 Sort

如果你想在 Vanilla 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"];
}

基本上,您需要将月份转换为数值。然后在此基础上进行排序。

这是3v4l上的工作演示

【讨论】:

  • 我期待 laravel eloquent collection 的解决方案
  • 你必须在 Laravel 中使用 PHP 来重新格式化数组。
  • 不,我们正在处理 laravel 集合,不期望转换为数组的开销。
  • @ShivarajRH 我已经更新了按 Laravel Collections 排序的答案。
  • 感觉用 date_parse() 有点慢,有什么最优的方法吗?
【解决方案3】:

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

$sort_collect = $result->sortBy(function ($item, $i) { 
    return ( Carbon::parse($item->monthname)->format("m") );
});
return $sort_collect;

返回所需的顺序, 谢谢@ascsoftw,Prafulla Kumar Sahu

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多