laravel按日期对数组进行排序 [英] laravel sort an array by date
本文介绍了laravel按日期对数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好朋友,我有一个看起来像这样的数组:
hello friends I have an array that looks like this:
array:3 [▼
0 => array:6 [▼
"date" => "2016-05-31 15:08:33"
0 => "31 May 16"
1 => "aze"
2 => "2"
3 => "hi"
4 => "487841464704194.jpg"
]
1 => array:6 [▼
"date" => "2016-05-31 15:26:09"
0 => "31 May 16"
1 => "aze"
2 => "2"
3 => "hey"
4 => "487841464704194.jpg"
]
2 => array:6 [▼
"date" => "2016-06-01 11:33:06"
0 => "01 Jun 16"
1 => "aze"
2 => "2"
3 => "Dm me please"
4 => "487841464704194.jpg"
]
]
我的目标是按date
对其进行排序.所以从新到旧.
My goal is to sort it by the date
. So from new to old.
如果尝试这样做:
$comarrSorted = $comarr->sortByDesc('date');
dd($comarrSorted);
但是我收到这个讨厌的错误:
But I get this nasty error:
Call to a member function sortByDesc() on array
任何人都可以帮助我吗?我猜该错误是由于它是一个收集函数引起的?使用此函数无法对我的数组进行排序吗?
Anyone can help me out? I guess the error is caused because it's a collection function? Is it not possible to sort my array with this function?
非常感谢!
推荐答案
您必须创建自己的函数
array_sort_by_column($array, 'date');
function array_sort_by_column(&$array, $column, $direction = SORT_ASC) {
$reference_array = array();
foreach($array as $key => $row) {
$reference_array[$key] = $row[$column];
}
array_multisort($reference_array, $direction, $array);
}
这篇关于laravel按日期对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文