//待排序数组
$arr_data = array(
array('id'=>1,'name'=>'D','money'=>10),
array('id'=>3,'name'=>'B','money'=>2),
array('id'=>4,'name'=>'C','money'=>5),
array('id'=>2,'name'=>'A','money'=>4),
);
//打印排序前数组
print_r($arr_data);
foreach($arr_data as $key => $val){
$sortName[$key]=$val['name'];
$sortMoney[$key]=$val['money'];
}
/*
排序顺序标志:
SORT_ASC – 按照上升顺序排序
SORT_DESC – 按照下降顺序排序
排序类型标志:
SORT_REGULAR – 将项目按照通常方法比较
SORT_NUMERIC – 将项目按照数值比较
SORT_STRING – 将项目按照字符串比较
每个数组之后不能指定两个同类的排序标志。每个数组后指定的排序标志仅对该数组有效,默认值 SORT_ASC 和 SORT_REGULA。
*/
//array_multisort($sortName,SORT_DESC, $sortMoney, $arr_data); //可选参数SORT_DESC
array_multisort($sortName, $sortMoney, $arr_data);
//打印排序后数组
print_r($arr_data);
执行结果:
//打印排序前数组
Array
(
[0] => Array
(
[id] => 1
[name] => D
[money] => 10
)
[1] => Array
(
[id] => 3
[name] => B
[money] => 2
)
[2] => Array
(
[id] => 4
[name] => C
[money] => 5
)
[3] => Array
(
[id] => 2
[name] => A
[money] => 4
)
)
//打印排序后数组
Array
(
[0] => Array
(
[id] => 2
[name] => A
[money] => 4
)
[1] => Array
(
[id] => 3
[name] => B
[money] => 2
)
[2] => Array
(
[id] => 4
[name] => C
[money] => 5
)
[3] => Array
(
[id] => 1
[name] => D
[money] => 10
)
)