0
$data['rows'][1] = array('id' => '1', 'cell' => array('branch' => 'ANT', 'type' =>     'deliver', 'dateTime' => '2014-02-07 01:01:01'));
$data['rows'][2] = array('id' => '2', 'cell' => array('branch' => 'ANT', 'type' =>     'deliver', 'dateTime' => '2014-02-06 01:01:01'));
$data['rows'][3] = array('id' => '3', 'cell' => array('branch' => 'GB2', 'type' =>     'deliver', 'dateTime' => '2014-02-07 02:02:02'));

I want to sort $data['rows'] descending based on dateTime. How can I do that?

Sharanya Dutta
  • 3,981
  • 2
  • 17
  • 27
Alex Coroza
  • 1,747
  • 3
  • 23
  • 39
  • Before answers come flooding in, if you are getting this data out of the database it would be easier to order it at that point (`order by dateTime desc`). If not then no problem – webnoob Feb 07 '14 at 07:02
  • have look here http://stackoverflow.com/questions/96759/how-do-i-sort-a-multidimensional-array-in-php –  Feb 07 '14 at 07:02
  • @webnoob these array came from 4 tables so i have to sort it in an array. is there a way to sort an array in php just like mysql does? – Alex Coroza Feb 07 '14 at 07:09

1 Answers1

2

The PHP function usort will serve your purpose:

usort($data["rows"], function($a, $b){
return strcmp($a["cell"]["dateTime"], $b["cell"]["dateTime"]);
});

Note that the above code will sort the array in ascending order. For descending order replace function($a, $b) with function($b, $a).

Sharanya Dutta
  • 3,981
  • 2
  • 17
  • 27