<?php
$b = array(
"2" => array(
"id" => 2,
"type" => 1,
"percent" => "90.00",
"status" => "normal"
),
"5" => array(
"id" => 5,
"type" => 3,
"percent" => "0.00",
"status" => "normal"
),
"4" => array(
"id" => 4,
"type" => 2,
"percent" => "10.00",
"status" => "normal"
),
);
function array_sort($arr, $keys, $type = array())//$type排序asc/desc或排序字典数组
{
$key_value = $new_array = array();
foreach ($arr as $k => $v) {
$key_value[$k] = $v[$keys];
}
if(is_array($type)){
uasort($key_value, function($a, $b) use($type){
$i = array_search($a, $type);
$j = array_search($b, $type);
if($i == $j)return 0;
return $i < $j ? -1 : 1;
});
}else{
if ($type == 'asc') {
asort($key_value);
} else {
arsort($key_value);
}
}
reset($key_value);
foreach ($key_value as $k => $v) {
$new_array[$k] = $arr[$k];
}
return $new_array;
}
//$c = array_sort($b, 'type', 'desc');
//$c = array_sort($b, 'type', 'asc');
$a = array(1,3,2);//按字典排序
$c = array_sort($b, 'type', $a);
print_r($c);