I have an array looking like this:
Array(
['some_first_category'] => Array(
['some_first_name'] => Array(
[0]=>'[email protected]',
[1]=>'[email protected]',
[2]=>'[email protected]',
[3]=>'[email protected]' )
['some_second_name'] => Array (
[1]=>'[email protected]',
[2]=>'[email protected]')
['some_third_name'] => Array(
[1]=>'[email protected]',
[2]=>'[email protected]',
[3]=>'[email protected]',
[4]=>'[email protected]' )
['some_second_category'] => Array(
['some_first_name'] => Array(
[0]=>'[email protected]' )
['some_second_name'] => Array(
[1]=>'[email protected]',
[2]=>'[email protected]',
[3]=>'[email protected]',
[4]=>'[email protected]')
['some_third_name'] => Array(
[1]=>'[email protected]',
[2]=>'[email protected]'))
And I want to sort the array by the number of values of that has the names, In my case I want to become this array:
Array(
['some_first_category'] => Array(
['some_third_name'] => Array(
[1]=>'[email protected]',
[2]=>'[email protected]',
[3]=>'[email protected]',
[4]=>'[email protected]' )
['some_first_name'] => Array(
[0]=>'[email protected]',
[1]=>'[email protected]',
[2]=>'[email protected]',
[3]=>'[email protected]' )
['some_second_name'] => Array (
[1]=>'[email protected]',
[2]=>'[email protected]')
['some_second_category'] => Array(
['some_second_name'] => Array(
[1]=>'[email protected]',
[2]=>'[email protected]',
[3]=>'[email protected]',
[4]=>'[email protected]')
['some_third_name'] => Array(
[1]=>'[email protected]',
[2]=>'[email protected]')
['some_first_name'] => Array(
[0]=>'[email protected]' ))
This means sorting categories by name by the number(count) of values of the names. Someone can help me? Thanks in advance,
Aäron
All you need is uasort
uasort($list, function ($a, $b) {
$a = count($a);
$b = count($b);
return ($a == $b) ? 0 : (($a < $b) ? -1 : 1);
});
Full Example
$list = Array(
'some_first_category' => Array(
'some_first_name' => Array(
0=>'[email protected]',
1=>'[email protected]',
2=>'[email protected]',
3=>'[email protected]' ),
'some_second_name' => Array (
1=>'[email protected]',
2=>'[email protected]'),
'some_third_name' => Array(
1=>'[email protected]',
2=>'[email protected]',
3=>'[email protected]',
4=>'[email protected]' )
),
'some_second_category' => Array(
'some_first_name' => Array(
0=>'[email protected]' ),
'some_second_name' => Array(
1=>'[email protected]',
2=>'[email protected]',
3=>'[email protected]',
4=>'[email protected]'),
'some_third_name' => Array(
1=>'[email protected]',
2=>'[email protected]'))
);
$list = array_map(function ($v) {
uasort($v, function ($a, $b) {
$a = count($a);
$b = count($b);
return ($a == $b) ? 0 : (($a < $b) ? 1 : - 1);
});
return $v;
}, $list);
print_r($list);
Output
Array
(
[some_first_category] => Array
(
[some_first_name] => Array
(
[0] => [email protected]
[1] => [email protected]
[2] => [email protected]
[3] => [email protected]
)
[some_third_name] => Array
(
[1] => [email protected]
[2] => [email protected]
[3] => [email protected]
[4] => [email protected]
)
[some_second_name] => Array
(
[1] => [email protected]
[2] => [email protected]
)
)
[some_second_category] => Array
(
[some_second_name] => Array
(
[1] => [email protected]
[2] => [email protected]
[3] => [email protected]
[4] => [email protected]
)
[some_third_name] => Array
(
[1] => [email protected]
[2] => [email protected]
)
[some_first_name] => Array
(
[0] => [email protected]
)
)
)
Using uksort:
uksort($yourArray, function($a, $b) { return count($b) - count($a); });
Using array_multisort:
array_multisort(array_map('count', $yourArray), SORT_DESC, $yourArray);
and you can see uasort as well
best of luck :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With