Sorting array by count of subarray

前端 未结 3 1926
情歌与酒
情歌与酒 2021-01-14 01:06

I have an array looking like this:

Array(
   [\'some_first_category\'] => Array(
            [\'some_first_name\'] => Array(
                                   


        
3条回答
  •  囚心锁ツ
    2021-01-14 01:28

    All you need is uasort

    uasort($list, function ($a, $b) {
        $a = count($a);
        $b = count($b);
        return ($a == $b) ? 0 : (($a < $b) ? -1 : 1);
    });
    

    Full Example

    $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);
    

    Output

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

提交回复
热议问题