简体   繁体   English

多维数组排序php

[英]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. 我用usort尝试过,但是没有用。

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;

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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