首页 > 代码库 > 二维数组排序

二维数组排序

<?php

$arr = array(
‘d‘ => array(‘id‘ => 3, ‘name‘ => 1, ‘age‘ => 7),
‘b‘ => array(‘id‘ => 6,‘name‘ => 3,‘age‘ => 4),
‘a‘ => array(‘id‘ => 8,‘name‘ => 10,‘age‘ => 5),
‘c‘ => array(‘id‘ => 5,‘name‘ => 2,‘age‘ => 2)
);

function multi_array_sort($arr,$id,$sort=SORT_DESC){ //倒序SORT_DESC、正序SORT_ASC
if(is_array($arr)){
foreach ($arr as $v){
if(is_array($v)){
$newarr[] = $v[$id];
}else{
return false;
}
}
}else{
return false;
}
array_multisort($newarr,$sort,$arr); //array_multisort返回排序数组
return $arr;
}

var_dump(multi_array_sort($arr,‘id‘));

?>

二维数组排序