3

I have an array that looks like this:

Array ( [0] => Array ( [filters] => Array ( [filter_1] => 1 
                                            [filter_2] => 1 
                                            [filter_3] => 1 
                                            [filter_4] => 1 
                                            [filter_5] => 1 
                                            [filter_6] => 1 ), 
                       [count]   => 2),

        [1] => Array ( [filters] => Array ( [filter_1] => 1 
                                            [filter_2] => 1 
                                            [filter_3] => 1 
                                            [filter_4] => 1 
                                            [filter_5] => 1 
                                            [filter_6] => 1 ), 
                       [count]   => 34)

        [2] => Array ( [filters] => Array ( [filter_1] => 1 
                                            [filter_2] => 1 
                                            [filter_3] => 1 
                                            [filter_4] => 1 
                                            [filter_5] => 1 
                                            [filter_6] => 1 ), 
                        [count]   => 7)

Is it possible to sort the main array keys by the [count] key in each, descending? So that they are in the following order: 1 -2 -0

2 Answers 2

5

You'll need to use the uasort function. Try something like this:

function cmp($a, $b) {
    if ($a["count"] == $b["count"]) {
        return 0;
    }
    return ($a["count"] > $b["count"]) ? -1 : 1;
}

uasort($array, 'cmp');
Sign up to request clarification or add additional context in comments.

Comments

4

You definitely want the usort function. You can define how the sort function determines which is larger or smaller.

If it's alright that each sub-array (of "filters" and "count") gets re-indexed, this should work perfectly. By re-indexed, I mean the newly sorted array would start at 0, progress to 1, etc. This is almost always how you want it, unless your original array is associative.

For example:

usort($array, "byCount");

function byCount($a, $b)
{
    if( $a['count'] == $b['count'] )
    {
        return 0;
    }
    return ($a['count'] < $b['count']) ? -1 : 1;
}

5 Comments

You could just use uasort to maintain key associations
@Jack Murdoch, assuming @soren would ever want to access the array by index, uasort would be the exact opposite of what he would want. If $array[0] got sorted after $array[1], the resulting array would be $array( 1, 0 ) which is rarely useful, unless, like I mentioned in my answer, the array is associative.
@rockerest Agreed, the only reason that I included this was because the OP mentioned that he wanted them to end up in the order "1 -2 -0".
@Jack, that's a good point. I kind of glossed over that. I'm assuming that was just an example to show how he would want it to sort, but you bring up a good point.
@rockerest He accepted yours, so obviously it was just for example ;)

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.