<?php
$arr = [
[
'id'=> 20,
'name' => 'a',
'type' =>[
'id'=> 5,
'type_name' => '学生'
]
],
[
'id'=> 10,
'name' => 'c',
'type' =>[
'id'=> 1,
'type_name' => '老师'
]
],
[
'id'=> 30,
'name' => 'e',
'type' =>[
'id'=> 8,
'type_name' => '老师'
]
],
[
'id'=> 18,
'name' => 'b',
'type' =>[
'id'=> 9,
'type_name' => '学生'
]
],
];
$arr_sort = array();
foreach($arr as $key=>$value){
$arr_sort['sort'][$key] = $value['type']['id'];
}
/*
array_multisort:
排序第一个数组, 顺序同时作用到第二个数组
实现了多维数组的排序!
这里以第三维的type_id排序
*/
echo '<pre>';
$rs = array_multisort($arr_sort['sort'], SORT_DESC, $arr);
var_dump($arr);
结果
array(4) {
[0]=>
array(3) {
["id"]=>
int(18)
["name"]=>
string(1) "b"
["type"]=>
array(2) {
["id"]=>
int(9)
["type_name"]=>
string(6) "学生"
}
}
[1]=>
array(3) {
["id"]=>
int(30)
["name"]=>
string(1) "e"
["type"]=>
array(2) {
["id"]=>
int(8)
["type_name"]=>
string(6) "老师"
}
}
[2]=>
array(3) {
["id"]=>
int(20)
["name"]=>
string(1) "a"
["type"]=>
array(2) {
["id"]=>
int(5)
["type_name"]=>
string(6) "学生"
}
}
[3]=>
array(3) {
["id"]=>
int(10)
["name"]=>
string(1) "c"
["type"]=>
array(2) {
["id"]=>
int(1)
["type_name"]=>
string(6) "老师"
}
}
}