按子数组计数对数组进行排序 [英] Sorting array by count of subarray

查看:120
本文介绍了按子数组计数对数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an array looking like this:

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

我想按具有名称的值的数量对数组进行排序,就我而言,我想成为该数组:

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

这意味着按名称按名称值的数量(数量)对类别进行排序.有人可以帮我吗? 预先感谢,

This means sorting categories by name by the number(count) of values of the names. Someone can help me? Thanks in advance,

亚伦

推荐答案

您需要的只是 uasort

All you need is 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
                )

        )

)

这篇关于按子数组计数对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆