按值对多维数组进行排序

时间:2016-05-04 11:06:05

标签: php

如何通过降序值对多维数组进行排序?我已经想出了这个但它没有用。

$data = [

  0 => array(
    "Date" => "2016-05-04 12:00:00"
  ),

  1 => array(
    "Date" => "2016-05-04 10:00:00"
  )

]

PHP

uasort($data, function($a, $b) {
  return $a['Date'] - $b['Date'];
});

2 个答案:

答案 0 :(得分:0)

试试这个:

$data = array(
  '0' => array(
    "Date" => "2016-05-04 12:00:00"
  ),
  '1' => array(
    "Date" => "2016-05-04 10:00:00"
  )
);

function date_compare($a, $b)
{
    $t1 = strtotime($a['Date']);
    $t2 = strtotime($b['Date']);
    return $t1 - $t2;
}    
usort($data, 'date_compare');
print '<pre>';print_r($data);
exit;

答案 1 :(得分:0)

试试这个:

uasort($data, function($a, $b) {
    $t1 = strtotime($a['Date']);
    $t2 = strtotime($b['Date']);
    return $t2 - $t1;
});