根据一个无序数组来对二维数组指定列排序
$arr1 = [ ['id'=>10086,'name'=>'移动'], ['id'=>10010,'name'=>'联通'], ['id'=>10000,'name'=>'电信'], ['id'=>110,'name'=>'公安局'], ['id'=>119,'name'=>'消防局'], ['id'=>120,'name'=>'急救'], ]; $arr2 = [120,119,110,10010]; foreach($arr1 as $v){ if(($pos = array_search($v['id'], $arr2))!== false){ $arr3[$pos][] = $v; }else{ $arr3[count($arr2)+1][] = $v; } } ksort($arr3); $arr = []; foreach($arr3 as $v){ $arr = array_merge($arr, array_values($v)); } print_r($arr3); print_r($arr);
最后输出如下(按照$arr2排序):
Array ( [0] => Array ( [id] => 120 [name] => 急救 ) [1] => Array ( [id] => 119 [name] => 消防局 ) [2] => Array ( [id] => 110 [name] => 公安局 ) [3] => Array ( [id] => 10010 [name] => 联通 ) [4] => Array ( [id] => 10086 [name] => 移动 ) [5] => Array ( [id] => 10000 [name] => 电信 ) )