php二维数组按其中某一列排序。
<?php $data = [ ['name' => '张三', 'age' => '20', 'score' => '78'], ['name' => '李四', 'age' => '21', 'score' => '78'], ['name' => '王五', 'age' => '20', 'score' => '60'], ['name' => '大强子', 'age' => '26', 'score' => '100'] ];
// 将数据根据 score 降序排列 array_multisort(array_column($data, 'score'), SORT_DESC, $data); echo '<pre>';print_r($data);
打印结果如下:
Array
(
[0] => Array
(
[name] => 大强子
[age] => 26
[score] => 100
)
[1] => Array
(
[name] => 张三
[age] => 20
[score] => 78
)
[2] => Array
(
[name] => 李四
[age] => 21
[score] => 78
)
[3] => Array
(
[name] => 王五
[age] => 20
[score] => 60
)
)
php二维数组按其中某几列(多列)排序。
// 将数据根据 age 升序排列,根据 score 降序排列 array_multisort(array_column($data, 'age'), SORT_ASC, array_column($data, 'score'), SORT_DESC, $data); echo '<pre>';print_r($data);
打印结果如下:
Array
(
[0] => Array
(
[name] => 张三
[age] => 20
[score] => 78
)
[1] => Array
(
[name] => 王五
[age] => 20
[score] => 60
)
[2] => Array
(
[name] => 李四
[age] => 21
[score] => 78
)
[3] => Array
(
[name] => 大强子
[age] => 26
[score] => 100
)
)