5

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

4
  • I see that you want to sort the second level ("name") by number of child elements descending, but do you also want to sort the first level ("category") by some logic? Commented Feb 5, 2013 at 10:01
  • It isn't sorted. It's just a display specific to your needs. Commented Feb 5, 2013 at 10:02
  • I want also some one to wash my car! but if I washed and I failed, its ok to ask a friend to help me! Commented Feb 5, 2013 at 10:03
  • ppl should learn that this side is supposed to give support, not full solutions to questions. You and only you have to do the work, if you need help that's ok, but asking for the solution w/o trying anything it's just cheap. Commented Feb 5, 2013 at 10:06

3 Answers 3

17

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

        )

)
Sign up to request clarification or add additional context in comments.

4 Comments

dont put functions in parameters of functions. this is not jQuery :P
@dognose So you'd rather pointlessly assign the closure to a variable or clutter the global namespace with a function identifier then? The only reason to avoid inline closures is if you need to support <=5.2, if you need to do that you need a better job and/or host.
Thanks a lot Baba !! The usort function I already tried it myself but it doesn't maintain the key of the array's. Thanks for the help everybody!
@DaveRandom A Sort function is not related to the context imho. So I might reuse it, so yes, I would put it somewhere global because then I don't need to rewrite or copy/paste it.
5

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

2 Comments

Perfect thanks... array_multisort(array_map('count', $yourArray), SORT_DESC, $yourArray); this worked for me
uasort($yourArray, function($a, $b) { return count($b) - count($a); }); worked great for me. if you want to reverse the sort order you can swap $a and $b in the count functions.
1

You should use usort function. Refer here.

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

usort($array_name,'sort_sub')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.