phparrayssortingmultidimensional-array

Sort a 2-D array by a column


I have the following array.

$mem = array(
1 => array(
   0 => array(
     "start" => "2016-05-16 17:00:00",
     "end" => "2016-05-16 18:00:00"
   ),
   1 => array(
     "start" => "2016-05-16 16:10:00",
     "end" => "2016-05-16 16:40:00"
)));

I want to sort it by the start value, so that

1 => array(
  "start" => "2016-05-16 16:10:00",
  "end" => "2016-05-16 16:40:00"
)

will be the first element of the array.

I tried it with usort but it didn't work.


Solution

  • Try:

    usort($array[1], function($a, $b) {
    
        if ($a["start"] == $b["start"]) 
        {
           return 0;
        }
        return ($a["start"] < $b["start"]) ? -1 : 1;
    
    });