I have an array looking like this:
Array(
[\'some_first_category\'] => Array(
[\'some_first_name\'] => Array(
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=>'first@email.com',
1=>'second@email.com',
2=>'third@email.com',
3=>'fourth@email.com' ),
'some_second_name' => Array (
1=>'first@email.com',
2=>'second@email.com'),
'some_third_name' => Array(
1=>'first@email.com',
2=>'second@email.com',
3=>'third@email.com',
4=>'fourth@email.com' )
),
'some_second_category' => Array(
'some_first_name' => Array(
0=>'first@email.com' ),
'some_second_name' => Array(
1=>'first@email.com',
2=>'second@email.com',
3=>'third@email.com',
4=>'fourth@email.com'),
'some_third_name' => Array(
1=>'first@email.com',
2=>'second@email.com'))
);
$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] => first@email.com
[1] => second@email.com
[2] => third@email.com
[3] => fourth@email.com
)
[some_third_name] => Array
(
[1] => first@email.com
[2] => second@email.com
[3] => third@email.com
[4] => fourth@email.com
)
[some_second_name] => Array
(
[1] => first@email.com
[2] => second@email.com
)
)
[some_second_category] => Array
(
[some_second_name] => Array
(
[1] => first@email.com
[2] => second@email.com
[3] => third@email.com
[4] => fourth@email.com
)
[some_third_name] => Array
(
[1] => first@email.com
[2] => second@email.com
)
[some_first_name] => Array
(
[0] => first@email.com
)
)
)