I have the below multidimesional array.
我有下面的multidimesional数组。
Array
(
[2] => Array
(
[66] => Array
(
[id] => 66
[count] => 9
)
[255] => Array
(
[id] => 255
[count] => 20
)
)
[1] => Array
(
[59] => Array
(
[id] => 59
[count] => 14
)
[255] => Array
(
[id] => 255
[count] => 73
)
)
)
I want to sort the inner array by value of count key in descending order. How can I achieve it ?
我想按降序排列count键的内部数组。我怎样才能实现它?
Thanks.
谢谢。
2 个解决方案
#1
2
try below solution:
尝试以下解决方案
<?php
$array = Array
(
'2' => Array
(
'66' => Array
(
'id' => 66 ,
'count' => 9
),
'255' => Array
(
'id' => 255,
'count' => 20
)
),
'1' => Array
(
'59' => Array
(
'id' => 59,
'count' => 14
),
'255' => Array
(
'id' => 255,
'count' => 73
)
)
);
echo '<pre>';
foreach($array as &$ar){
usort($ar, function($a, $b) {
return $b['count'] - $a['count'];
});
}
print_r($array);
Output:
输出:
Array
(
[2] => Array
(
[0] => Array
(
[id] => 255
[count] => 20
)
[1] => Array
(
[id] => 66
[count] => 9
)
)
[1] => Array
(
[0] => Array
(
[id] => 255
[count] => 73
)
[1] => Array
(
[id] => 59
[count] => 14
)
)
)
#2
-1
Here is an example:
这是一个例子:
Sort Multi-Dimensional Array By Value In PHP https://paulund.co.uk/sort-multi-dimensional-array-value
按PHP在值中排序多维数组https://paulund.co.uk/sort-multi-dimensional-array-value
#1
2
try below solution:
尝试以下解决方案
<?php
$array = Array
(
'2' => Array
(
'66' => Array
(
'id' => 66 ,
'count' => 9
),
'255' => Array
(
'id' => 255,
'count' => 20
)
),
'1' => Array
(
'59' => Array
(
'id' => 59,
'count' => 14
),
'255' => Array
(
'id' => 255,
'count' => 73
)
)
);
echo '<pre>';
foreach($array as &$ar){
usort($ar, function($a, $b) {
return $b['count'] - $a['count'];
});
}
print_r($array);
Output:
输出:
Array
(
[2] => Array
(
[0] => Array
(
[id] => 255
[count] => 20
)
[1] => Array
(
[id] => 66
[count] => 9
)
)
[1] => Array
(
[0] => Array
(
[id] => 255
[count] => 73
)
[1] => Array
(
[id] => 59
[count] => 14
)
)
)
#2
-1
Here is an example:
这是一个例子:
Sort Multi-Dimensional Array By Value In PHP https://paulund.co.uk/sort-multi-dimensional-array-value
按PHP在值中排序多维数组https://paulund.co.uk/sort-multi-dimensional-array-value