How do I sort a multi-dimensional array by time values in PHP? -
i'm doing school project , have multi-dimensional array having start_time
, end_time
of courses.
i sorted array day, want sort array time. such lowest start_time first element of array.
this how array @ moment:
array ( [0] => array ( [courseid] => comp345 [lectureid] => ss [day] => monday [stime] => 18:20 [etime] => 20:30 [term] => winter [year] => 2014 ) [1] => array ( [courseid] => comp275 [lectureid] => gg [day] => monday [stime] => 12:15 [etime] => 15:16 [term] => winter [year] => 2014 ) )
i wondering if there pre-defined functions or if need create new specific function task .
i can access values of start_time :
foreach ($array $element) { $start_time = (substr($element['stime'], 0, 5)); }
this return time in format : 08:20
it works same way normal numbers when comparing such :
08:20 < 10:15 = true
08:20 > 10:15 = false
php >= 5.5.0:
array_multisort(array_map('strtotime', array_column($array, 'stime')), sort_asc, $array);
php < 5.5.0:
foreach ($array $k => $v) { $stime[$k] = strtotime($v['stime']); } array_multisort($stime, sort_asc, $array);
Comments
Post a Comment