4

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

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

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

亚伦

4

3 回答 3

16

您只需要 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
                )

        )

)
于 2013-02-05T10:05:11.660 回答
5

使用uksort

uksort($yourArray, function($a, $b) { return count($b) - count($a); });

使用array_multisort:

array_multisort(array_map('count', $yourArray), SORT_DESC, $yourArray);

你也可以看到uasort

祝你好运:)

于 2016-08-31T12:48:54.850 回答
1

您应该使用 usort 功能。参考这里

function sort_sub($a,$b)
{
$res= count($b)-count($a);
return $res;
}

usort($array_name,'sort_sub')
于 2013-02-05T10:05:14.827 回答