gpt4 book ai didi

php - 按子数组的计数对数组进行排序

转载 作者:可可西里 更新时间:2023-10-31 23:53:49 24 4
gpt4 key购买 nike

我有一个看起来像这样的数组:

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' ))

这意味着根据名称值的数量(计数)按名称对类别进行排序。有人可以帮助我吗?提前致谢,

亚伦

最佳答案

您只需要 uasort

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
)

)

)

关于php - 按子数组的计数对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14704634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com