按子数组计数降序对多维数组进行排序并保留第一级键
我有一个数组,例如:
$array = [
'DEF' => [
['type' => 1, 'id' => 1212, 'name' => 'Jane Doe', 'current' => 1],
['type' => 1, 'id' => 3123121, 'name' => 'Door', 'current' => null],
],
'ABC' => [
['type' => 1, 'id' => 1234, 'name' => 'John Doe', 'current' => null],
],
'WW' => [
['type' => 1, 'id' => 1212, 'name' => 'Jane Doe', 'current' => 1],
['type' => 1, 'id' => 3123121, 'name' => 'Door', 'current' => null],
['type' => 1, 'id' => 64646, 'name' => 'Floor', 'current' => null],
]
];
并且我想按内部数组项的数量( count() )降序(即首先是大多数项)对这个数组进行排序,所以我将拥有这个数组:
[
'WW' => [
['type' => 1, 'id' => 1212, 'name' => 'Jane Doe', 'current' => 1],
['type' => 1, 'id' => 3123121, 'name' => 'Door', 'current' => null],
['type' => 1, 'id' => 64646, 'name' => 'Floor', 'current' => null],
],
'DEF' => [
['type' => 1, 'id' => 1212, 'name' => 'Jane Doe', 'current' => 1],
['type' => 1, 'id' => 3123121, 'name' => 'Door', 'current' => null],
],
'ABC' => [
['type' => 1, 'id' => 1234, 'name' => 'John Doe', 'current' => null],
]
];
任何人都可以建议一种有效的方法吗?谢谢。
I have an array such as:
$array = [
'DEF' => [
['type' => 1, 'id' => 1212, 'name' => 'Jane Doe', 'current' => 1],
['type' => 1, 'id' => 3123121, 'name' => 'Door', 'current' => null],
],
'ABC' => [
['type' => 1, 'id' => 1234, 'name' => 'John Doe', 'current' => null],
],
'WW' => [
['type' => 1, 'id' => 1212, 'name' => 'Jane Doe', 'current' => 1],
['type' => 1, 'id' => 3123121, 'name' => 'Door', 'current' => null],
['type' => 1, 'id' => 64646, 'name' => 'Floor', 'current' => null],
]
];
And I want to sort this array by number ( count() ) of inner-array items descending (i.e. most items first), so I will have this array:
[
'WW' => [
['type' => 1, 'id' => 1212, 'name' => 'Jane Doe', 'current' => 1],
['type' => 1, 'id' => 3123121, 'name' => 'Door', 'current' => null],
['type' => 1, 'id' => 64646, 'name' => 'Floor', 'current' => null],
],
'DEF' => [
['type' => 1, 'id' => 1212, 'name' => 'Jane Doe', 'current' => 1],
['type' => 1, 'id' => 3123121, 'name' => 'Door', 'current' => null],
],
'ABC' => [
['type' => 1, 'id' => 1234, 'name' => 'John Doe', 'current' => null],
]
];
Can anyone suggest an efficient way to do so? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
uksort
:使用
array_multisort
:使用 PHP 5.3:
Using
uksort
:Using
array_multisort
:With PHP < 5.3:
请注意,如果任何数组值本身不是数组,这将会中断,您可能需要在某处添加
is_array()
检查...Note that this will break if any of the array values are not themselves arrays, you may want to add an
is_array()
check somewhere...如果您只担心按大小排序而不担心实际质量,则可以使用最简单的方法进行降序排序并使用
arsort()
保留键。<一href="https://www.php.net/manual/en/language.operators.comparison.php#:%7E:text=Array%20with%20fewer%20members%20is%20smaller%2C%20if%20key%20from %20操作数%20 1%20is%20not%20found%20in%20operand%202%20then%20arrays%20are%20incomparable%2C%20otherwise%20%2D%20compare%20value%20by%20value%20(参见%20following%20示例)" rel="nofollow noreferrer">成员较少的数组较小,如果操作数 2 中未找到操作数 1 中的键,则数组不可比较,否则 - 逐个比较值(请参阅以下示例)
如果有多行大小相同,那么它们将根据实际数据进一步降序排序。
代码:(演示)
If you are only worried about sorting on size and not the actual quality, you can use the simplest approach to sort descending and preserve keys with
arsort()
.Array with fewer members is smaller, if key from operand 1 is not found in operand 2 then arrays are incomparable, otherwise - compare value by value (see following example)
In the event that you have multiple rows with the same size, then they will be further sorted descending based on their actual data.
Code: (Demo)