如何在数组中的最新日期后排序?
这是我的数组($ testarray)的输出:
Array (
[0] => Array ( [created] => 16-02-13 20:41:56 [restaurant_id] => 64324 [title] => Café Blabla [city] => State K )
[1] => Array ( [created] => 19-02-13 13:42:14 [restaurant_id] => 42132 [title] => Chicos Blabla [city] => State K )
[2] => Array ( [created] => 17-02-13 19:41:30 [restaurant_id] => 51242 [title] => Restaurant Blabla [city] => State K )
[3] => Array ( [created] => 18-02-13 16:42:12 [restaurant_id] => 64342 [title] => Couloir Blabla [city] => State S )
答案 0 :(得分:0)
试试这个:
<?php
$arr=your array;
$sort = array();
foreach($arr as $k=>$v) {
$sort['created'][$k] = $v['created'];
}
array_multisort($sort['created'], SORT_DESC, $arr);
echo "<pre>";
print_r($arr);
?>
答案 1 :(得分:0)
您可以使用asort()
或ksort()
对数组进行排序。
你可以在这里学习
http://php.net/manual/en/array.sorting.php
答案 2 :(得分:0)
usort
允许您通过提供函数回调基于自定义方法进行排序:
// Sorts two array elements based on the value of the
// `[created]` element.
function SortByDateCreatedDate($x,$y){
$xd = $x['created']; //or if they're strings:*/ strtotime($x['created']);
$yd = $y['created']; //or if they're strings:*/ strtotime($y['created']);
return $xd > $yd ? 1
: $yd > $xd ? -1
: 0;
}
$testarray = /*...*/;
usort($testarray, 'SortByCreatedDate');
答案 3 :(得分:0)
<?php
$dts = array_map(function($x) { $x['created']; }, $array);
$max = max($dts);
$idx = array_search($max, $dts);
$do_not_sort = array_slice($array, 0, $idx);
$do_sort = array_slice($array, $idx);
function cmp($x, $y) {
$a = $x['created'];
$b = $y['created'];
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
uasort($do_sort, 'cmp');
$sorted[] = $do_not_sort;
$sorted[] = $do_sort;
?>