Search code examples
phparrayssortingmultidimensional-array

Sort the items of a multidimensional array by a specific column value within the first subarray


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
            )
        )
        [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.


Solution

  • 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.