php - Sort Multi Dimensional Array efficiently -


here content of array:

array(   'june 01, 2014' => array(     (int) 722107 => array(         'date' => 'june 01, 2014',         'start_time' => '2:00 am',         'end_time' => '03:00 am'     ),     (int) 117646 => array(         'date' => 'june 01, 2014',         'start_time' => '12:30 am',         'end_time' => '03:30 am'     )   ),   'may 31, 2014' => array(     (int) 769349 => array(         'date' => 'may 31, 2014',         'start_time' => '12:30 am',         'end_time' => '03:30 am'     )   ) ) 

and output want should sorted first keys (date in asc), sort again values of each keys start time in asc.

based on example above should output:

array(      'may 31, 2014' => array(     (int) 769349 => array(         'date' => 'may 31, 2014',         'start_time' => '12:30 am',         'end_time' => '03:30 am'     )  ), 'june 01, 2014' => array(      (int) 117646 => array(         'date' => 'june 01, 2014',         'start_time' => '12:30 am',         'end_time' => '03:30 am'     ),     (int) 722107 => array(         'date' => 'june 01, 2014',         'start_time' => '2:00 am',         'end_time' => '03:00 am'     )    )      ) 

the 2 passes can uksort() , uasort() using custom function compare 2 dates:

function timecompare($a, $b) {     $va = strtotime($a);     $vb = strtotime($b);      if ($va != $vb) {         return $va < $vb ? -1 : 1;     }     return 0; }  // first pass (sort outer elements) uksort($data, 'timecompare'); // second pass (sort inner elements) foreach ($data &$item) {     uasort($item, function($a, $b) {         return timecompare($a['start_time'], $b['start_time']);     }); } 

demo


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -