我有一个数组如下:
Array
(
[0] => Array
(
[id] => 2
[type] => comment
[batch] => Array
(
[0] => Array
(
[id] => T001
[datetime] => 2010-05-15 11:29:00
)
[1] => Array
(
[id] => T003
[datetime] => 2010-05-15 11:33:00
)
)
)
[1] => Array
(
[id] => 3
[type] => status
[batch] => Array
(
[0] => Array
(
[id] => T002
[datetime] => 2010-05-15 11:31:00
)
[1] => Array
(
[id] => T004
[datetime] => 2010-05-15 12:32:00
)
[2] => Array
(
[id] => T006
[datetime] => 2010-05-15 12:33:00
)
)
)
[2] => Array
(
[id] => 4
[type] => status
[batch] => Array
(
[0] => Array
(
[id] => T005
[datetime] => 2010-05-15 12:34:00
)
[1] => Array
(
[id] => T007
[datetime] => 2010-05-15 13:35:00
)
[2] => Array
(
[id] => T008
[datetime] => 2010-05-15 10:36:00
)
)
)
)
我想通过仅子阵列中的最后日期时间重新排序数组[批量]
结果应该是这样的:
Array
(
[0] => Array
(
[id] => 4
[type] => status
[batch] => Array
(
[0] => Array
(
[id] => T005
[datetime] => 2010-05-15 12:34:00
)
[1] => Array
(
[id] => T007
[datetime] => 2010-05-15 13:35:00
)
[2] => Array
(
[id] => T008
[datetime] => 2010-05-15 10:36:00
)
)
)
[1] => Array
(
[id] => 2
[type] => comment
[batch] => Array
(
[0] => Array
(
[id] => T001
[datetime] => 2010-05-15 11:29:00
)
[1] => Array
(
[id] => T003
[datetime] => 2010-05-15 11:33:00
)
)
)
[2] => Array
(
[id] => 3
[type] => status
[batch] => Array
(
[0] => Array
(
[id] => T002
[datetime] => 2010-05-15 11:31:00
)
[1] => Array
(
[id] => T004
[datetime] => 2010-05-15 12:32:00
)
[2] => Array
(
[id] => T006
[datetime] => 2010-05-15 12:33:00
)
)
)
)
我可以使用" array_multisort&#34 ;?请建议。 非常感谢你!
答案 0 :(得分:1)
使用usort
,它允许您使用用户定义的比较函数对数组进行排序。 (PHP Docs)
usort($array, function($a, $b) {
$lastTimestampA = end($a['batch']);
$lastTimestampB = end($b['batch']);
return strtotime($lastTimestampA['datetime']) > strtotime($lastTimestampB['datetime']);
})