微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 按子阵列的数量排序数组

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

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
                )

        )

)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐