当前位置:网站首页>【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)
}
}
边栏推荐
- ssh连win10报错:Permission denied (publickey,keyboard-interactive).
- Beidou navigation technology and industrial application of "chasing dreams in space and feeling for Beidou"
- 两步处理字符串正则匹配得到JSON列表
- 5. < tag stack and general problems > supplement: lt.946 Verify the stack sequence (the same as the push in and pop-up sequence of offer 31. stack)
- Transport layer TCP protocol and UDP protocol
- C# 40. Byte[] to hexadecimal string
- ECCV 2020 double champion team, take you to conquer target detection on the 7th
- Codeforces Round #802 (Div. 2)(A-D)
- vscode config
- cartographer_fast_correlative_scan_matcher_2d分支定界粗匹配
猜你喜欢

Ad tutorial series | 4 - creating an integration library file

Installation and deployment of alluxio
![[unity3d] human computer interaction input](/img/4d/47f6d40bb82400fe9c6d624c8892f7.png)
[unity3d] human computer interaction input

cartographer_ pose_ graph_ 2d
![[red team] what preparations should be made to join the red team?](/img/03/f246f18f8925167dbd5e9d63912faa.png)
[red team] what preparations should be made to join the red team?

Recursively traverse directory structure and tree presentation

百度API地图的标注不是居中显示,而是显示在左上角是怎么回事?已解决!

cartographer_pose_graph_2d
Briefly describe the pitfalls of mobile IM development: architecture design, communication protocol and client

Baidu API map is not displayed in the middle, but in the upper left corner. What's the matter? Resolved!
随机推荐
Sentimentin tensorflow_ analysis_ cell
PHP 2D / multidimensional arrays are sorted in ascending and descending order according to the specified key values
CMakeLists. txt Template
cartographer_ local_ trajectory_ builder_ 2d
thread priority
redis探索之布隆过滤器
第九章 设置结构化日志记录(一)
Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?
LSTM in tensorflow_ Layers actual combat
UWB ultra high precision positioning system architecture
[upsampling method opencv interpolation]
Apktool tool usage document
关于支付接口回调地址参数字段是“notify_url”,签名过后的特殊字符url编码以后再解码后出现错误(¬ , ¢, ¤, £)
AD教程系列 | 4 - 创建集成库文件
Learn from small samples and run to the sea of stars
Installation and deployment of alluxio
cartographer_fast_correlative_scan_matcher_2d分支定界粗匹配
Thoughts triggered by the fact that app applications are installed on mobile phones and do not display icons
国务院发文,完善身份认证、电子印章等应用,加强数字政府建设
Keras actual combat cifar10 in tensorflow