我有一个数组,如下所示:
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'))
我想根据有名称的值的数量对数组进行排序,在我的情况下,我想成为这个数组:
Array(
['some_first_category'] => Array(
['some_third_name'] => Array(
[1]=>'first@email.com',
[2]=>'second@email.com',
[3]=>'third@email.com',
[4]=>'fourth@email.com' )
['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_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' ))
这意味着根据名称值的数量(计数)按名称对类别进行排序。有人能帮我吗?提前感谢
Aäron
您只需要uasor
uasort($list, function ($a, $b) {
$a = count($a);
$b = count($b);
return ($a == $b) ? 0 : (($a < $b) ? -1 : 1);
});
完整示例
$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);
输出
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
)
)
)
使用uksort:
uksort($yourArray, function($a, $b) { return count($b) - count($a); });
使用array_multi-sort:
array_multisort(array_map('count', $yourArray), SORT_DESC, $yourArray);
你可以看到uasort和
祝你好运:)
您应该使用usort函数。请参阅此处。
function sort_sub($a,$b)
{
$res= count($b)-count($a);
return $res;
}
usort($array_name,'sort_sub')