这是CSDN上的一个人写的,我测试了一下,是真的强大,可以,以后PHP数组排序靠这一个function就行,不用看官网那一大堆的函数。
最近需要做一个三维排序,查了一下网上,貌似没有一个比较万能的方法,所以就自己思考写了一下,分享一下~
主要依赖方法:array_multisort 方法文档:https://www.php.net/manual/zh/function.array-multisort.php
方法如下 参数:第一个为要排序的数组,剩下是要排序的键(key),和排序方法,键的话因为要应对多维的情况,所以需要上下级连接,我这里采用”.”连接,(multi_dimension_sort($arr, ‘id’, SORT_ASC, ‘class.class_num’, SORT_ASC, ‘class.student.value’, SORT_DESC)),参数可自行增减,具体看下面的例子:
function multi_dimension_sort(...$args){
$arr = array_shift($args); // 取到要排序的数组,剩下的为要排序的键和排序类型
$sort_arg = [];
foreach($args as $arg){
// 这里主要是为了得到排序的key对应的值
$sort = $arr;
if(is_string($arg)){
$arg = explode('.', $arg); // 我设定参数里面多维数组下的键,用‘.’连接下级的键,这里得到键,然后下面循环取得数组$arr里面该键对应的值
foreach($arg as $key){
$sort = array_column($sort, $key); // 每次循环$sort的维度就会减一
}
$sort_arg[] = $sort;
}else{
$sort_arg[] = $arg; // 排序方法SORT_ASC、SORT_DESC等,代码来自文曦博客:www.vience.cn
}
}
$sort_arg[] = &$arr; // 这个数组大致结构为:[$sort, SORT_ASC, $sort2, SORT_DESC,$arr]
call_user_func_array('array_multisort', $sort_arg); // 因为参数不确定数量,所以不能直接array_multisort($sort, SORT_ASC, $sort2, SORT_DESC,$arr),用call_user_func_array执行
return($arr);
}
示例:构建一个由学院、班级、学生组成的多维数组,实现按照学院、班级、分数依次来排序
$arr = [
[
'id' => 2,
'name' => '学院2',
'class' => [
'class_num' => 2,
'student' => [
'student_id' => 5,
'value' => 20,
]
],
],[
'id' => 2,
'name' => '学院2',
'class' => [
'class_num' => 2,
'student' => [
'student_id' => 6,
'value' => 10,
]
],
],[
'id' => 2,
'name' => '学院2',
'class' => [
'class_num' => 3,
'student' => [
'student_id' => 4,
'value' => 30,
]
],
],[
'id' => 1,
'name' => '学院1',
'class' => [
'class_num' => 2,
'student' => [
'student_id' => 3,
'value' => 10,
]
],
],[
'id' => 1,
'name' => '学院1',
'class' => [
'class_num' => 2,
'student' => [
'student_id' => 4,
'value' => 5,
]
],
],[
'id' => 1,
'name' => '学院1',
'class' => [
'class_num' => 3,
'student' => [
'student_id' => 4,
'value' => 10,
]
],
],[
'id' => 2,
'name' => '学院2',
'class' => [
'class_num' => 2,
'student' => [
'student_id' => 7,
'value' => 20,
]
],
],[
'id' => 2,
'name' => '学院2',
'class' => [
'class_num' => 2,
'student' => [
'student_id' => 1,
'value' => 5,
]
],
]
];
dump(multi_dimension_sort($arr, 'id', SORT_ASC, 'class.class_num', SORT_ASC, 'class.student.value', SORT_DESC));
运行结果:
array:8 [
0 => array:3 [
"id" => 1
"name" => "学院1"
"class" => array:2 [
"class_num" => 2
"student" => array:2 [
"student_id" => 3
"value" => 10
]
]
]
1 => array:3 [
"id" => 1
"name" => "学院1"
"class" => array:2 [
"class_num" => 2
"student" => array:2 [
"student_id" => 4
"value" => 5
]
]
]
2 => array:3 [
"id" => 1
"name" => "学院1"
"class" => array:2 [
"class_num" => 3
"student" => array:2 [
"student_id" => 4
"value" => 10
]
]
]
3 => array:3 [
"id" => 2
"name" => "学院2"
"class" => array:2 [
"class_num" => 2
"student" => array:2 [
"student_id" => 5
"value" => 20
]
]
]
4 => array:3 [
"id" => 2
"name" => "学院2"
"class" => array:2 [
"class_num" => 2
"student" => array:2 [
"student_id" => 7
"value" => 20
]
]
]
5 => array:3 [
"id" => 2
"name" => "学院2"
"class" => array:2 [
"class_num" => 2
"student" => array:2 [
"student_id" => 6
"value" => 10
]
]
]
6 => array:3 [
"id" => 2
"name" => "学院2"
"class" => array:2 [
"class_num" => 2
"student" => array:2 [
"student_id" => 1
"value" => 5
]
]
]
7 => array:3 [
"id" => 2
"name" => "学院2"
"class" => array:2 [
"class_num" => 3
"student" => array:2 [
"student_id" => 4
"value" => 30
]
]
]
]
转载出处链接: https://blog.csdn.net/qq_27396589/article/details/90448429
© 版权声明
THE END
暂无评论内容