开发者

Sorting Multidimensional Array by Specific Key

EDIT: For anyone who might come across this post with a similar problem, It was solved by taking konforce's supplied answer and tweaking around a bit with the custom sorting function:

function cmp($a, $b) {
    if ($a[5] == $b[5]) {
        return ($a[3] < $b[3]) ? -1 :1;
    }
    return ($a[5] > $b[5]) ? -1 : 1;
}

Notice $a[5] == $b[5] does not return zero. It was changed to check who has the most losses and then sort it in ASC order. I'm sure you can even keep going and add another if-statement in there in-case they have the same losses.

Lastly, all you do is usort($ARRAY, "cmp"); and finito!!!

Original Post

My apologies for coming up with yet another MD Array sorting question but I'm just not getting it. I've searched aplenty for a solution and although many sites have provided what seemed like a logical answer I still have not been able to figure it out.

My problem is since I'm still learning its been rather difficult for me to grasp the concept of using usort with a custom comparing function. Atleast, thats what I have seen the most when others have tried to sort MD Arrays.

I'm working on a small project to sharpen up on my php skills. Its a very basic tournament standings script that holds a team's information within an array. I would like to sort the array by most points($array[X][X][5]).

So the array looks something like this:

Array (
        [0] => Array (
            [0] => Array (
                [0] => cooller
                [1] => 6 
                [2] => 6 
                [3] => 0 
                [4] => 0 
                [5] => 18 
            )
        ) 
        [1] => Array (
            [0] => Array (
                [0] => strenx 
                [1] => 9 
                [2] => 5 
                [3] => 1 
                [4] => 3 
                [5] => 18
            )
        )
        [2] => Array (
            [0] => Array (
                [0] => rapha
                [1] => 10
                [2] => 8
                [3] => 1
                [4] => 1
                [5] => 25
            )
        ) [3] => Array (
            [0] => Array (
                [0] => ronald reagan
                [1] => 5 
                [2] => 4 
                [3] => 0 
                [4] => 1 
                [5] => 13
            )
        )
    )

I would like to sort it by most points(cell #5), so it would look like this after sorting:

Array (
        [0] => Array (
            [0] => Array (
                [0] => rapha
                [1] => 10
                [2] => 8
                [3] => 1
                [4] => 1
                [5] => 25
       开发者_StackOverflow社区     )
        )
        [1] => Array (
            [0] => Array (
                [0] => cooller
                [1] => 6 
                [2] => 6 
                [3] => 0 
                [4] => 0 
                [5] => 18 
            )
        ) 
        [2] => Array (
            [0] => Array (
                [0] => strenx 
                [1] => 9 
                [2] => 5 
                [3] => 1 
                [4] => 3 
                [5] => 18
            )
        )
        [3] => Array (
            [0] => Array (
                [0] => ronald reagan
                [1] => 5 
                [2] => 4 
                [3] => 0 
                [4] => 1 
                [5] => 13
            )
        )
    )

The player with 25 points would be at the top, followed by 18, 18, and lastly 13. Sorry for my earlier post, was having difficulty wording my question correctly. Thanks in advanced!


I think you want something like this:

usort($standings, function($a, $b) { return $b[0][5] - $a[0][5]; });

Or prior to PHP 5.3:

function cmp($a, $b) { return $b[0][5] - $a[0][5]; }
usort($standings, 'cmp');

When using usort, the $a and $b parameters will be one "layer" into the supplied array. So in your case, an example of $a or $b will be:

[0] => Array (
         [0] => cooller
         [1] => 6 
         [2] => 6 
         [3] => 0 
         [4] => 0 
         [5] => 18 
)

I'm not sure why you have an extra containing array there, but as you can see, you want to sort based on the [0][5] position.


usort($standings[0][0][5], 'cmp') won't work because the first parameter isn't an array to sort, it's just a single number, the points.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜