Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting array by count of subarray

I have an array looking like this:

Array(
   ['some_first_category'] => Array(
            ['some_first_name'] => Array(
                           [0]=>'[email protected]',
                           [1]=>'[email protected]',
                           [2]=>'[email protected]',
                           [3]=>'[email protected]' )
             ['some_second_name'] => Array (
                           [1]=>'[email protected]',
                           [2]=>'[email protected]')
             ['some_third_name'] => Array(
                           [1]=>'[email protected]',
                           [2]=>'[email protected]',
                           [3]=>'[email protected]',
                           [4]=>'[email protected]' )
   ['some_second_category'] => Array(
            ['some_first_name'] => Array(
                           [0]=>'[email protected]' )
             ['some_second_name'] => Array(
                           [1]=>'[email protected]',
                           [2]=>'[email protected]',
                           [3]=>'[email protected]',
                           [4]=>'[email protected]')
             ['some_third_name'] => Array(
                           [1]=>'[email protected]',
                           [2]=>'[email protected]'))

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]=>'[email protected]',
                           [2]=>'[email protected]',
                           [3]=>'[email protected]',
                           [4]=>'[email protected]' )
            ['some_first_name'] => Array(
                           [0]=>'[email protected]',
                           [1]=>'[email protected]',
                           [2]=>'[email protected]',
                           [3]=>'[email protected]' )
             ['some_second_name'] => Array (
                           [1]=>'[email protected]',
                           [2]=>'[email protected]')

   ['some_second_category'] => Array(
             ['some_second_name'] => Array(
                           [1]=>'[email protected]',
                           [2]=>'[email protected]',
                           [3]=>'[email protected]',
                           [4]=>'[email protected]')
             ['some_third_name'] => Array(
                           [1]=>'[email protected]',
                           [2]=>'[email protected]')
            ['some_first_name'] => Array(
                           [0]=>'[email protected]' ))

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

Aäron

like image 384
Aaron Avatar asked Nov 30 '22 13:11

Aaron


2 Answers

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=>'[email protected]',
                           1=>'[email protected]',
                           2=>'[email protected]',
                           3=>'[email protected]' ),
             'some_second_name' => Array (
                           1=>'[email protected]',
                           2=>'[email protected]'),
             'some_third_name' => Array(
                           1=>'[email protected]',
                           2=>'[email protected]',
                           3=>'[email protected]',
                           4=>'[email protected]' )
        ),
   'some_second_category' => Array(
            'some_first_name' => Array(
                           0=>'[email protected]' ),
             'some_second_name' => Array(
                           1=>'[email protected]',
                           2=>'[email protected]',
                           3=>'[email protected]',
                           4=>'[email protected]'),
             'some_third_name' => Array(
                           1=>'[email protected]',
                           2=>'[email protected]'))

    );

$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] => [email protected]
                    [1] => [email protected]
                    [2] => [email protected]
                    [3] => [email protected]
                )

            [some_third_name] => Array
                (
                    [1] => [email protected]
                    [2] => [email protected]
                    [3] => [email protected]
                    [4] => [email protected]
                )

            [some_second_name] => Array
                (
                    [1] => [email protected]
                    [2] => [email protected]
                )

        )

    [some_second_category] => Array
        (
            [some_second_name] => Array
                (
                    [1] => [email protected]
                    [2] => [email protected]
                    [3] => [email protected]
                    [4] => [email protected]
                )

            [some_third_name] => Array
                (
                    [1] => [email protected]
                    [2] => [email protected]
                )

            [some_first_name] => Array
                (
                    [0] => [email protected]
                )

        )

)
like image 85
Baba Avatar answered Dec 05 '22 16:12

Baba


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

like image 29
manish1706 Avatar answered Dec 05 '22 18:12

manish1706