当前位置:网站首页>【PHP】PHP二维数组按照多个字段进行排序
【PHP】PHP二维数组按照多个字段进行排序
2022-06-26 05:20:00 【weixin_43224306】
通过array_multisort 进行数组字段排序
这里也是本篇主要要说的。
官网对array_multisort函数的解释: 对多个数组或多维数组进行排序
bool array_multisort ( array $ar1 [, mixed $arg [, mixed $... [, array $... ]]] )
成功时返回 TRUE, 或者在失败时返回 FALSE.
array_multisort() 可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序。关联(string)键名保持不变,但数字键名会被重新索引。
输入数组被当成一个表的列并以行来排序——这类似于 SQL 的 ORDER BY 子句的功能。第一个数组是要排序的主要数组。数组中的行(值)比较为相同的话就按照下一个输入数组中相应值的大小来排序,依此类推。
本函数的参数结构有些不同寻常,但是非常灵活。第一个参数必须是一个数组。接下来的每个参数可以是数组或者是下面列出的排序标志。
排序顺序标志:
SORT_ASC - 按照上升顺序排序
SORT_DESC - 按照下降顺序排序
排序类型标志:
SORT_REGULAR - 将项目按照通常方法比较
SORT_NUMERIC - 将项目按照数值比较
SORT_STRING - 将项目按照字符串比较
每个数组之后不能指定两个同类的排序标志。每个数组后指定的排序标志仅对该数组有效 - 在此之前为默认值 SORT_ASC 和 SORT_REGULAR。
例1:
$ar1 = array("10", 101, 100, 6);
$ar2 = array(1, 3, 6, 5);
array_multisort($ar1, $ar2);
print_r($ar1);
print_r($ar2);
第二个数组中的项目顺序完全和第一个数组中相应的项目顺序一致。
Array
(
[0] => 6
[1] => 10
[2] => 100
[3] => 101
)
Array
(
[0] => 5
[1] => 1
[2] => 6
[3] => 3
)
例2:
$ar = array(
array("b10", 'c11', 101, 100, "a"),
array(1, 2, "2", 9, 5)
);
array_multisort($ar[0], SORT_ASC, SORT_STRING, $ar[1], SORT_NUMERIC, SORT_DESC);
print_r($ar);
经过排序后,第一个数组将包含 "b10", 'c11',101,100,"a"(作为字符串上升排序),第二个数组将包含 1,2,"2",9,5(作为数值下降排序)。
Array
(
[0] => Array
(
[0] => 100
[1] => 101
[2] => a
[3] => b10
[4] => c11
)
[1] => Array
(
[0] => 9
[1] => 2
[2] => 5
[3] => 1
[4] => 2
)
)
例3:(我们要的结果)
$data[] = array('customer_name' => '小李', 'money' => 12, 'distance' => 2, 'address' => '长安街C坊');
$data[] = array('customer_name' => '王晓', 'money' => 30, 'distance' => 10, 'address' => '北大街30号');
$data[] = array('customer_name' => '赵小雅', 'money' => 89, 'distance' => 6, 'address' => '解放路恒基大厦A座');
$data[] = array('customer_name' => '小月', 'money' => 150, 'distance' => 5, 'address' => '天桥十字东400米');
$data[] = array('customer_name' => '李亮亮', 'money' => 45, 'distance' => 26, 'address' => '天山西路198弄');
$data[] = array('customer_name' => '董娟', 'money' => 67, 'distance' => 17, 'address' => '新大南路2号');
// 取得列的列表
foreach ($data as $key => $row) {
$distance[$key] = $row['distance'];
$money[$key] = $row['money'];
}
array_multisort($distance, SORT_DESC, $data);
print_r($data);
$data包含有行的数组,但是 array_multisort() 需要一个包含列的数组,因此用foreach来取得列,然后排序。
所有,我们将array_multisort() 封装下:
/**
* 二维数组根据某个字段排序
* @param array $array 要排序的数组
* @param string $keys 要排序的键字段
* @param string $sort 排序类型 SORT_ASC SORT_DESC
* @return array 排序后的数组
*/
function arraySort($array, $keys, $sort = SORT_DESC) {
$keysValue = [];
foreach ($array as $k => $v) {
$keysValue[$k] = $v[$keys];
}
array_multisort($keysValue, $sort, $array);
return $array;
}
继续使用上例中的$data 数组
# 按距离降序排序
$a = arraySort($data, 'distance', SORT_DESC);
print_r($a);
# 按money升序排序
$b = arraySort($data, 'money', SORT_ASC);
print_r($b);
效果:
Array
(
[0] => Array
(
[customer_name] => 李亮亮
[money] => 45
[distance] => 26
[address] => 天山西路198弄
)
[1] => Array
(
[customer_name] => 董娟
[money] => 67
[distance] => 17
[address] => 新大南路2号
)
[2] => Array
(
[customer_name] => 王晓
[money] => 30
[distance] => 10
[address] => 北大街30号
)
[3] => Array
(
[customer_name] => 赵小雅
[money] => 89
[distance] => 6
[address] => 解放路恒基大厦A座
)
[4] => Array
(
[customer_name] => 小月
[money] => 150
[distance] => 5
[address] => 天桥十字东400米
)
[5] => Array
(
[customer_name] => 小李
[money] => 12
[distance] => 2
[address] => 长安街C坊
)
)
Array
(
[0] => Array
(
[customer_name] => 李亮亮
[money] => 45
[distance] => 26
[address] => 天山西路198弄
)
[1] => Array
(
[customer_name] => 董娟
[money] => 67
[distance] => 17
[address] => 新大南路2号
)
[2] => Array
(
[customer_name] => 王晓
[money] => 30
[distance] => 10
[address] => 北大街30号
)
[3] => Array
(
[customer_name] => 赵小雅
[money] => 89
[distance] => 6
[address] => 解放路恒基大厦A座
)
[4] => Array
(
[customer_name] => 小月
[money] => 150
[distance] => 5
[address] => 天桥十字东400米
)
[5] => Array
(
[customer_name] => 小李
[money] => 12
[distance] => 2
[address] => 长安街C坊
)
)
Array
(
[0] => Array
(
[customer_name] => 小李
[money] => 12
[distance] => 2
[address] => 长安街C坊
)
[1] => Array
(
[customer_name] => 王晓
[money] => 30
[distance] => 10
[address] => 北大街30号
)
[2] => Array
(
[customer_name] => 李亮亮
[money] => 45
[distance] => 26
[address] => 天山西路198弄
)
[3] => Array
(
[customer_name] => 董娟
[money] => 67
[distance] => 17
[address] => 新大南路2号
)
[4] => Array
(
[customer_name] => 赵小雅
[money] => 89
[distance] => 6
[address] => 解放路恒基大厦A座
)
[5] => Array
(
[customer_name] => 小月
[money] => 150
[distance] => 5
[address] => 天桥十字东400米
)
<?php
$arr = array(
array(
'a' => 1,
'b' => 3,
'c' => 1
),
array(
'a' => 6,
'b' => 1,
'c' => 1
),
array(
'a' => 5,
'b' => 3,
'c' => 1
),
array(
'a' => 1,
'b' => 2,
'c' => 2,
),
array(
'a' => 1,
'b' => 2,
'c' => 3,
),
array(
'a' => 1,
'b' => 2,
'c' => 1,
)
);
foreach ( $arr as $key => $row ){
$a[$key] = $row ['a'];
$b[$key] = $row ['b'];
$c[$key] = $row ['c'];
}
array_multisort($a, SORT_ASC, $b, SORT_ASC,$c, SORT_ASC, $arr);
var_dump($arr);
结果:
array(6) {
[0]=>
array(3) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(1)
}
[1]=>
array(3) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(2)
}
[2]=>
array(3) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
}
[3]=>
array(3) {
["a"]=>
int(1)
["b"]=>
int(3)
["c"]=>
int(1)
}
[4]=>
array(3) {
["a"]=>
int(5)
["b"]=>
int(3)
["c"]=>
int(1)
}
[5]=>
array(3) {
["a"]=>
int(6)
["b"]=>
int(1)
["c"]=>
int(1)
}
}
边栏推荐
- Official image acceleration
- 【ARM】在NUC977上搭建基于boa的嵌入式web服务器
- Schematic diagram of UWB ultra high precision positioning system
- [upsampling method opencv interpolation]
- [leetcode] 713: subarray with product less than k
- Collections and dictionaries
- Installation and deployment of alluxio
- tensorlow:cifar100_ train
- uni-app吸顶固定样式
- How to ensure the efficiency and real-time of pushing large-scale group messages in mobile IM?
猜你喜欢
Recursively traverse directory structure and tree presentation
Status of processes and communication between processes
红队得分方法统计
Second day of deep learning and tensorfow
Codeforces Round #802 (Div. 2)(A-D)
FastAdmin Apache下设置伪静态
Technical past: tcp/ip protocol that has changed the world (precious pictures, caution for mobile phones)
Learn from small samples and run to the sea of stars
2. < tag dynamic programming and conventional problems > lt.343 integer partition
【活动推荐】云原生、产业互联网、低代码、Web3、元宇宙……哪个是 2022 年架构热点?...
随机推荐
Procedural life
What is UWB in ultra-high precision positioning system
C# 40. byte[]与16进制string互转
The best Chinese open source class of vision transformer, ten hours of on-site coding to play with the popular model of Vit!
Schematic diagram of UWB ultra high precision positioning system
瀚高数据库自定义操作符‘!~~‘
The beautiful scenery is natural, and the wonderful pen is obtained by chance -- how is the "wonderful pen" refined?
[unity3d] rigid body component
Create SSH key pair configuration steps
Technical problems to be faced in mobile terminal im development
Classic theory: detailed explanation of three handshakes and four waves of TCP protocol
The wechat team disclosed that the wechat interface is stuck with a super bug "15..." The context of
Installation and deployment of alluxio
SSH connected to win10 and reported an error: permission denied (publickey, keyboard interactive)
Tp5.0框架 PDO连接mysql 报错:Too many connections 解决方法
Day4 branch and loop jobs
[upsampling method opencv interpolation]
【Unity3D】刚体组件Rigidbody
定位设置水平,垂直居中(多种方法)
cartographer_ fast_ correlative_ scan_ matcher_ 2D branch and bound rough matching