DeTechn Blog

php二维数组按字段排序

<?php


$data = [
    ['status'=>1,'addtime'=>'20191212'],
    ['status'=>2,'addtime'=>'20191210'],
    ['status'=>1,'addtime'=>'20191211'],
    ['status'=>1,'addtime'=>'20191209'],
    ['status'=>1,'addtime'=>'20191228'],
];
//简单的
$simpletime = array_column($data,'addtime');
array_multisort($simpletime,SORT_DESC,$data);
echo "<pre>";
print_r($data);
echo "</pre>";
//复杂的
foreach ( $data as $key => $row ){
    $status[$key] = $row ['status'];
    $addtime[$key] = $row ['addtime'];
}
array_multisort($status, SORT_DESC, $addtime, SORT_DESC, $data);
echo "<pre>";
var_dump($data);
echo "</pre>";

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »