-2

How would you sort a multidimensional array by descending value? I've come up with this but it doesn't function.

$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'];
});
JmC
  • 21
  • 5
  • 3
    Possible duplicate of [How can I sort arrays and data in PHP?](http://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php) – Epodax May 04 '16 at 11:07

2 Answers2

0

Try this:

$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;
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27
0

Try this:

uasort($data, function($a, $b) {
    $t1 = strtotime($a['Date']);
    $t2 = strtotime($b['Date']);
    return $t2 - $t1;
});
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32