// 二维数组排序算法函数,能够具有通用性,可以调用php内置函数。
function array_sort(&$arr, $order=[]){
$result=[];
if (empty($arr)) {
return $result;
}
uasort($arr, function ($a, $b) use ($order) {
foreach ($order as $key=> $sort) {
array_shift($order);
if ($a[$key]==$b[$key]) {
continue;
}
if ($sort===’DESC’) {
return ($a[$key] > $b[$key]) ? -1 : 1;
} else {
return ($a[$key] > $b[$key]) ? 1 : -1;
}
}
return 0;
});
foreach ($arr as $value) {
$result[]=$value;
}
return $arr=$result;
}
// 测试
$ps=array(
array(‘id’=> 1, ‘name’=> ‘zhangcan’, ‘age’=> 13),
array(‘id’=> 2, ‘name’=> ‘lisi’, ‘age’=> 16),
array(‘id’=> 3, ‘name’=> ‘wangwu’, ‘age’=> 17),
array(‘id’=> 4, ‘name’=> ‘wangwu’, ‘age’=> 18),
);
// $ret=array_sort($person, [‘name’=> ‘ASC’]);
$ret=array_sort($ps, [‘name’=>’ASC’, ‘age’=>’DESC’]);
print_r($ret);