如何将PHP二维数组按自定义方法排序?

2026-04-02 03:111阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计2227个文字,预计阅读时间需要9分钟。

如何将PHP二维数组按自定义方法排序?

根据您的需求,以下是修改后的开头内容,不超过100字:

根据二维数组中的某个字段进行排序,例如我下面的数组是一组满减信息,我要按照满减额度从小到大排序。

php$arr=[ [amount=> 60, discount=> 35], [amount=> 20, discount=> 10], // 其他元素...];

1、根据二维数组中的某个字段进行排序

比如我下面的数组是一组满减折扣的信息,我要按照满减的金额从小到大排序

<?php $arr =[ ["amount"=> 60,"discount"=> 35], ["amount"=> 20,"discount"=> 10], ["amount"=> 5,"discount"=> 3] ]; array_multisort( array_column($arr, 'amount'), SORT_ASC, $arr ); ?>

2、根据二维数组中的多个字段进行排序

比如对一个班级里面的同学,先按年龄降序排序,年龄相同的,再按 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

3、使用usort()函数自定义排序二维数组

<?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; //从大到小排序 } });

PHP 数组排序函数汇总

主要区别有:

有些函数基于 array 的键来排序, 而其他的基于值来排序的:$array['key'] = 'value';。

排序之后键和值之间的关联关系是否能够保持, 是指排序之后数组的键可能 会被重置为数字型的(0,1,2 ...)。

排序的顺序有:字母表顺序, 升序(由低到高), 降序(由高到低),数字排序,自然排序,随机顺序或者用户自定义排序。

注意:下列的所有排序函数都是直接作用于数组本身, 而不是返回一个新的有序的数组。

以下函数对于数组中相等的元素,会保留原有的排序。 在 PHP 8.0.0 之前,它们排序后的顺序是未定义的(也即相等元素之间的顺序是不稳定的)。

排序函数属性

函数名称

排序依据

数组索引键保持

排序的顺序

相关函数

array_multisort()[1]


string 键保持不变,int 键重新索引

第一个数组或者由选项指定

array_walk()[2]

asort()[3]



升序

arsort()[4]

arsort()[5]



降序

asort()[6]

krsort()[7]



降序

ksort()[8]

ksort()[9]



升序

krsort()[10]

natcasesort()[11]



自然排序,大小写不敏感

natsort()[12]

natsort()[13]



自然排序

natcasesort()[14]

rsort()[15]

如何将PHP二维数组按自定义方法排序?



降序

sort()[16]

shuffle()[17]



随机

array_rand()[18]

sort()[19]



升序

rsort()[20]

uasort()[21]



由用户定义

uksort()[22]

uksort()[23]



由用户定义

uasort()[24]

usort()[25]



由用户定义

uasort()[26]

引用链接

[1]array_multisort():www.php.net/manual/zh/function.array-multisort.php
[2]array_walk():www.php.net/manual/zh/function.array-walk.php
[3]asort():www.php.net/manual/zh/function.asort.php
[4]arsort():www.php.net/manual/zh/function.arsort.php
[5]arsort():www.php.net/manual/zh/function.arsort.php
[6]asort():www.php.net/manual/zh/function.asort.php
[7]krsort():www.php.net/manual/zh/function.krsort.php
[8]ksort():www.php.net/manual/zh/function.ksort.php
[9]ksort():www.php.net/manual/zh/function.ksort.php
[10]krsort():www.php.net/manual/zh/function.krsort.php
[11]natcasesort():www.php.net/manual/zh/function.natcasesort.php
[12]natsort():www.php.net/manual/zh/function.natsort.php
[13]natsort():www.php.net/manual/zh/function.natsort.php
[14]natcasesort():www.php.net/manual/zh/function.natcasesort.php
[15]rsort():www.php.net/manual/zh/function.rsort.php
[16]sort():www.php.net/manual/zh/function.sort.php
[17]shuffle():www.php.net/manual/zh/function.shuffle.php
[18]array_rand():www.php.net/manual/zh/function.array-rand.php
[19]sort():www.php.net/manual/zh/function.sort.php
[20]rsort():www.php.net/manual/zh/function.rsort.php
[21]uasort():www.php.net/manual/zh/function.uasort.php
[22]uksort():www.php.net/manual/zh/function.uksort.php
[23]uksort():www.php.net/manual/zh/function.uksort.php
[24]uasort():www.php.net/manual/zh/function.uasort.php
[25]usort():www.php.net/manual/zh/function.usort.php
[26]uasort():www.php.net/manual/zh/function.uasort.php

本文共计2227个文字,预计阅读时间需要9分钟。

如何将PHP二维数组按自定义方法排序?

根据您的需求,以下是修改后的开头内容,不超过100字:

根据二维数组中的某个字段进行排序,例如我下面的数组是一组满减信息,我要按照满减额度从小到大排序。

php$arr=[ [amount=> 60, discount=> 35], [amount=> 20, discount=> 10], // 其他元素...];

1、根据二维数组中的某个字段进行排序

比如我下面的数组是一组满减折扣的信息,我要按照满减的金额从小到大排序

<?php $arr =[ ["amount"=> 60,"discount"=> 35], ["amount"=> 20,"discount"=> 10], ["amount"=> 5,"discount"=> 3] ]; array_multisort( array_column($arr, 'amount'), SORT_ASC, $arr ); ?>

2、根据二维数组中的多个字段进行排序

比如对一个班级里面的同学,先按年龄降序排序,年龄相同的,再按 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

3、使用usort()函数自定义排序二维数组

<?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; //从大到小排序 } });

PHP 数组排序函数汇总

主要区别有:

有些函数基于 array 的键来排序, 而其他的基于值来排序的:$array['key'] = 'value';。

排序之后键和值之间的关联关系是否能够保持, 是指排序之后数组的键可能 会被重置为数字型的(0,1,2 ...)。

排序的顺序有:字母表顺序, 升序(由低到高), 降序(由高到低),数字排序,自然排序,随机顺序或者用户自定义排序。

注意:下列的所有排序函数都是直接作用于数组本身, 而不是返回一个新的有序的数组。

以下函数对于数组中相等的元素,会保留原有的排序。 在 PHP 8.0.0 之前,它们排序后的顺序是未定义的(也即相等元素之间的顺序是不稳定的)。

排序函数属性

函数名称

排序依据

数组索引键保持

排序的顺序

相关函数

array_multisort()[1]


string 键保持不变,int 键重新索引

第一个数组或者由选项指定

array_walk()[2]

asort()[3]



升序

arsort()[4]

arsort()[5]



降序

asort()[6]

krsort()[7]



降序

ksort()[8]

ksort()[9]



升序

krsort()[10]

natcasesort()[11]



自然排序,大小写不敏感

natsort()[12]

natsort()[13]



自然排序

natcasesort()[14]

rsort()[15]

如何将PHP二维数组按自定义方法排序?



降序

sort()[16]

shuffle()[17]



随机

array_rand()[18]

sort()[19]



升序

rsort()[20]

uasort()[21]



由用户定义

uksort()[22]

uksort()[23]



由用户定义

uasort()[24]

usort()[25]



由用户定义

uasort()[26]

引用链接

[1]array_multisort():www.php.net/manual/zh/function.array-multisort.php
[2]array_walk():www.php.net/manual/zh/function.array-walk.php
[3]asort():www.php.net/manual/zh/function.asort.php
[4]arsort():www.php.net/manual/zh/function.arsort.php
[5]arsort():www.php.net/manual/zh/function.arsort.php
[6]asort():www.php.net/manual/zh/function.asort.php
[7]krsort():www.php.net/manual/zh/function.krsort.php
[8]ksort():www.php.net/manual/zh/function.ksort.php
[9]ksort():www.php.net/manual/zh/function.ksort.php
[10]krsort():www.php.net/manual/zh/function.krsort.php
[11]natcasesort():www.php.net/manual/zh/function.natcasesort.php
[12]natsort():www.php.net/manual/zh/function.natsort.php
[13]natsort():www.php.net/manual/zh/function.natsort.php
[14]natcasesort():www.php.net/manual/zh/function.natcasesort.php
[15]rsort():www.php.net/manual/zh/function.rsort.php
[16]sort():www.php.net/manual/zh/function.sort.php
[17]shuffle():www.php.net/manual/zh/function.shuffle.php
[18]array_rand():www.php.net/manual/zh/function.array-rand.php
[19]sort():www.php.net/manual/zh/function.sort.php
[20]rsort():www.php.net/manual/zh/function.rsort.php
[21]uasort():www.php.net/manual/zh/function.uasort.php
[22]uksort():www.php.net/manual/zh/function.uksort.php
[23]uksort():www.php.net/manual/zh/function.uksort.php
[24]uasort():www.php.net/manual/zh/function.uasort.php
[25]usort():www.php.net/manual/zh/function.usort.php
[26]uasort():www.php.net/manual/zh/function.uasort.php