Sorting array by count of subarray

前端 未结 3 1927
情歌与酒
情歌与酒 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
                    )
    
            )
    
    )
    
    0 讨论(0)
  • 2021-01-14 01:47

    Using uksort:

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

    Using array_multisort:

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

    and you can see uasort as well

    best of luck :)

    0 讨论(0)
  • 2021-01-14 01:50

    You should use usort function. Refer here.

    function sort_sub($a,$b)
    {
    $res= count($b)-count($a);
    return $res;
    }
    
    usort($array_name,'sort_sub')
    
    0 讨论(0)
提交回复
热议问题