php 数组时间排序 array_multisort



date_default_timezone_set('Asia/Shanghai');
$arr = array(
    'a'=>array(
           'uid'=>12,
           'name'=>'zhangsan1',
           'ctime'=>'2018-03-20 11:28:00'
                ),
    'b'=>array(
        'uid'=>13,
        'name'=>'zhangsan2',
        'ctime'=>'2018-03-19 11:28:00'
    ),
    'v'=>array(
        'uid'=>14,
        'name'=>'zhangsan3',
        'ctime'=>'2018-03-21 11:28:00'
    ),
    'c'=>array(
        'uid'=>14,
        'name'=>'zhangsan3',
        'ctime'=>'2018-03-22 11:28:00'
    ),
);
$ctime_str = array();
foreach($arr as $key=>$v){
     
    $arr[$key]['ctime_str'] = strtotime($v['ctime']);
    $ctime_str[] = $arr[$key]['ctime_str'];
}

                                                
array_multisort($ctime_str,SORT_DESC,$arr);  // 批注:$ctime_str 是第一个数组,类似于sql排序中的,先以谁为准,order by id,ctime  ||| $arr是第二个数组
print_r($arr);

运行结果:

Array
(
   [c] => Array
       (
           [uid] => 14
           [name] => zhangsan3
           [ctime] => 2018-03-22 11:28:00
           [ctime_str] => 1521689280
       )
   [v] => Array
       (
           [uid] => 14
           [name] => zhangsan3
           [ctime] => 2018-03-21 11:28:00
           [ctime_str] => 1521602880
       )
   [a] => Array
       (
           [uid] => 12
           [name] => zhangsan1
           [ctime] => 2018-03-20 11:28:00
           [ctime_str] => 1521516480
       )
   [b] => Array
       (
           [uid] => 13
           [name] => zhangsan2
           [ctime] => 2018-03-19 11:28:00
           [ctime_str] => 1521430080
       )
)

你可能感兴趣的:(php)