Created
March 13, 2019 08:49
-
-
Save Jason-cqtan/a747be338a99124f9b1a7fb9a20897ca to your computer and use it in GitHub Desktop.
二维数组多字段排序
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$array1 = array( | |
0=>array('id'=>8,'name'=>'Apple','age'=> 18), | |
1=>array('id'=>8,'name'=>'Bed','age'=>17), | |
2=>array('id'=>5,'name'=>'Cos','age'=>16), | |
3=>array('id'=>5,'name'=>'Cos','age'=>14) | |
); | |
function sortArrByManyField(){ | |
$args = func_get_args(); | |
if(empty($args)){ | |
return null; | |
} | |
$arr = array_shift($args); | |
if(!is_array($arr)){ | |
throw new Exception("第一个参数不为数组"); | |
} | |
foreach($args as $key => $field){ | |
if(is_string($field)){ | |
$temp = array(); | |
foreach($arr as $index=> $val){ | |
$temp[$index] = $val[$field]; | |
} | |
$args[$key] = $temp; | |
} | |
} | |
$args[] = &$arr;//引用值 | |
call_user_func_array('array_multisort',$args); | |
return array_pop($args); | |
} | |
$arr = sortArrByManyField($array1,'id',SORT_ASC,'name',SORT_ASC,'age',SORT_DESC); | |
print_r($arr); | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment