简体   繁体   中英

Multidimensional array sort php

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.

try the following:

function date_compare($a, $b)
{
    $t1 = strtotime($a['start']);
    $t2 = strtotime($b['start']);
    return $t1 - $t2;
}    
usort($mem[1], 'date_compare');

Try:

usort($array[1], function($a, $b) {

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

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM