PHP按包含日期的元素对多维数组进行排序我有一个数组,如:Array([0] => Array
(
[id] => 2
[type] => comment [text] => hey [datetime] => 2010-05-15 11:29:45
)[1] => Array
(
[id] => 3
[type] => status [text] => oi [datetime] => 2010-05-26 15:59:53
)[2] => Array
(
[id] => 4
[type] => status [text] => yeww [datetime] => 2010-05-26 16:04:24
))有人能建议一种基于datetime元素排序/排序的方法吗?
3 回答
慕姐4208626
TA贡献1852条经验 获得超7个赞
usort()
function date_compare($a, $b){ $t1 = strtotime($a['datetime']); $t2 = strtotime($b['datetime']); return $t1 - $t2;} usort($array, 'date_compare');
编辑
usort
date_compare()
date_compare
"datetime"
0
$a
$b
usort()
波斯汪
TA贡献1811条经验 获得超4个赞
foreach ($originalArray as $key => $part) { $sort[$key] = strtotime($part['datetime']); } array_multisort($sort, SORT_DESC, $originalArray);
array_multisort(array_map('strtotime',array_column($originalArray,'datetime')), SORT_DESC, $originalArray);
- 3 回答
- 0 关注
- 850 浏览
添加回答
举报
0/150
提交
取消