比如我下面的数组是一组满减折扣的信息,我要按照满减的金额从小到大排序
<?php
$arr =[
["amount"=> 60,"discount"=> 35],
["amount"=> 20,"discount"=> 10],
["amount"=> 5,"discount"=> 3]
];
array_multisort(
array_column($arr, 'amount'),
SORT_ASC,
$arr
);
?>
比如对一个班级里面的同学,先按年龄降序排序,年龄相同的,再按 id 降序排序。
<?php
$array = [
["id" => "1", "name" => "a", "age" => "19",],
["id" => "2", "name" => "b", "age" => "39",],
["id" => "3", "name" => "c", "age" => "9",],
["id" => "4", "name" => "d", "age" => "1",],
["id" => "5", "name" => "e", "age" => "88",],
["id" => "6", "name" => "f", "age" => "65",],
["id" => "7", "name" => "g", "age" => "19",],
];
array_multisort(
array_column($array, "age"),
SORT_DESC,
array_column($array, "id"),
SORT_DESC,
$array
);
?>
如果遇到下面的PHP警告,就是你拿来比较的标志的数组大小跟待排序的数组大小不一致,可以把 array_multisort 里面的数组都打印出来看下大小是否一致
PHP Warning : array_multisort(): Array sizes are inconsistent
<?php
$a = [
['year' => 1992, 'month' => 4, 'num' => 3],
['year' => 1993, 'month' => 1, 'num' => 2],
['year' => 1992, 'month' => 2, 'num' => 1]
];
usort($a, function ($a_prev, $a_next) {
if ($a_prev['year'] != $a_next['year']) {
return ($a_prev['year'] > $a_next['year']) ? 1 : -1; //从小到大排序
//return ($a_prev['year'] > $a_next['year']) ? -1 : 1; //从大到小排序
} else {
if ($a_prev['month'] == $a_next['month']) return 0;
return ($a_prev['month'] > $a_next['month']) ? 1 : -1; //从小到大排序
//return ($a_prev['month'] > $a_next['month']) ? -1 : 1; //从大到小排序
}
});
主要区别有:
有些函数基于 array 的键来排序, 而其他的基于值来排序的:$array['key'] = 'value';。
排序之后键和值之间的关联关系是否能够保持, 是指排序之后数组的键可能 会被重置为数字型的(0,1,2 ...)。
排序的顺序有:字母表顺序, 升序(由低到高), 降序(由高到低),数字排序,自然排序,随机顺序或者用户自定义排序。
注意:下列的所有排序函数都是直接作用于数组本身, 而不是返回一个新的有序的数组。
以下函数对于数组中相等的元素,会保留原有的排序。 在 PHP 8.0.0 之前,它们排序后的顺序是未定义的(也即相等元素之间的顺序是不稳定的)。
排序函数属性
函数名称 | 排序依据 | 数组索引键保持 | 排序的顺序 | 相关函数 |
---|---|---|---|---|
array_multisort() | 值 | string 键保持不变,int 键重新索引 | 第一个数组或者由选项指定 | array_walk() |
asort() | 值 | 是 | 升序 | arsort() |
arsort() | 值 | 是 | 降序 | asort() |
krsort() | 键 | 是 | 降序 | ksort() |
ksort() | 键 | 是 | 升序 | krsort() |
natcasesort() | 值 | 是 | 自然排序,大小写不敏感 | natsort() |
natsort() | 值 | 是 | 自然排序 | natcasesort() |
rsort() | 值 | 否 | 降序 | sort() |
shuffle() | 值 | 否 | 随机 | array_rand() |
sort() | 值 | 否 | 升序 | rsort() |
uasort() | 值 | 是 | 由用户定义 | uksort() |
uksort() | 键 | 是 | 由用户定义 | uasort() |
usort() | 值 | 否 | 由用户定义 | uasort() |
[1]
array\_multisort(): https://www.php.net/manual/zh/function.array-multisort.php[2]
array\_walk(): https://www.php.net/manual/zh/function.array-walk.php[3]
asort(): https://www.php.net/manual/zh/function.asort.php[4]
arsort(): https://www.php.net/manual/zh/function.arsort.php[5]
arsort(): https://www.php.net/manual/zh/function.arsort.php[6]
asort(): https://www.php.net/manual/zh/function.asort.php[7]
krsort(): https://www.php.net/manual/zh/function.krsort.php[8]
ksort(): https://www.php.net/manual/zh/function.ksort.php[9]
ksort(): https://www.php.net/manual/zh/function.ksort.php[10]
krsort(): https://www.php.net/manual/zh/function.krsort.php[11]
natcasesort(): https://www.php.net/manual/zh/function.natcasesort.php[12]
natsort(): https://www.php.net/manual/zh/function.natsort.php[13]
natsort(): https://www.php.net/manual/zh/function.natsort.php[14]
natcasesort(): https://www.php.net/manual/zh/function.natcasesort.php[15]
rsort(): https://www.php.net/manual/zh/function.rsort.php[16]
sort(): https://www.php.net/manual/zh/function.sort.php[17]
shuffle(): https://www.php.net/manual/zh/function.shuffle.php[18]
array\_rand(): https://www.php.net/manual/zh/function.array-rand.php[19]
sort(): https://www.php.net/manual/zh/function.sort.php[20]
rsort(): https://www.php.net/manual/zh/function.rsort.php[21]
uasort(): https://www.php.net/manual/zh/function.uasort.php[22]
uksort(): https://www.php.net/manual/zh/function.uksort.php[23]
uksort(): https://www.php.net/manual/zh/function.uksort.php[24]
uasort(): https://www.php.net/manual/zh/function.uasort.php[25]
usort(): https://www.php.net/manual/zh/function.usort.php[26]
uasort(): https://www.php.net/manual/zh/function.uasort.php