0

如何在我的数组中的最新日期之后排序?

这是我的数组($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 )
4

4 回答 4

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);

?>
于 2013-02-20T13:29:23.070 回答
0

您可以使用asort()or对数组进行排序ksort()

你可以在这里学习

http://php.net/manual/en/array.sorting.php
于 2013-02-20T13:30:26.417 回答
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');
于 2013-02-20T13:31:54.780 回答
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;

?>
于 2013-02-20T13:56:12.087 回答