I have an array for a league table. The array holds TeamName, GoalDifference, Points. I sort the array on Points using arsort, but, if two teams have the same number of points, I'd then like to sort them according to GoalDifference.

I'm sure this must have been answered many times before, but I've been search the web for the last 2 days to try to find the answer without success, so if anyone can help, I'd be very grateful.

Thanks.

    This should do it:

    foreach ($arr as $key => $val) {
        $tmp_pts[$key] = $val['Points'];
        $tmp_diff[$key] = $val['GoalDifference'];
    }
    array_multisort($tmp_pts, SORT_DESC, $tmp_diff, SORT_DESC, $arr);

      Thanks very much for the reply, but I've tried it, and it doesn't seem to do anything atol to the order of my array.

      My array is named $teams, so I changed $arr in the foreach and array_multisort lines to $teams.

      I then output $teams using
      foreach ($teams["points"] as $key =>$value)
      but the array still seems to be in the same order as it was to start with.

      Sorry to be a pest, but could you confirm how I can display the array in the sorted order once it has been sorted?

      Thanks again.

        If your array is structured like this:

        Array
        (
            [0] => Array
                (
                    [Team] => Team_A
                    [Points] => 4
                    [GoalDifference] => 2
                )
        
        [1] => Array
            (
                [Team] => Team_B
                [Points] => 8
                [GoalDifference] => 4
            )
        
        [2] => Array
            (
                [Team] => Team_C
                [Points] => 4
                [GoalDifference] => 4
            )
        
        )

        (which is what I gathered from reading your first post.)

        Then after running this code:

        foreach ($teams as $key => $val) { 
            $tmp_pts[$key] = $val['Points']; 
            $tmp_diff[$key] = $val['GoalDifference']; 
        } 
        array_multisort($tmp_pts, SORT_DESC, $tmp_diff, SORT_DESC, $teams);
        print_r($teams);

        You will see this:

        Array
        (
            [0] => Array
                (
                    [Team] => Team_B
                    [Points] => 8
                    [GoalDifference] => 4
                )
        
        [1] => Array
            (
                [Team] => Team_C
                [Points] => 4
                [GoalDifference] => 4
            )
        
        [2] => Array
            (
                [Team] => Team_A
                [Points] => 4
                [GoalDifference] => 2
            )
        
        )

        The array is sorted primarily on "Team" and secondarily on "GoalDifference".

        If it doesn't work for you, after making the necessary variable name changes (including capitalization), then you'll need to post an accurate description of your array structure, and/or an example of it.

          Another alternative is to use [man]usort[/man] or [man]uasort/man.

          function mySort($a, $b)
          {
             if($a['Points'] == $b['Points'])
             {
                return $b['GoalDifference'] - $a['GoalDifference'];
             }
             return $b['Points'] - $a['Points'];
          }
          usort($leagueResultsArray, 'mySort');
          // use uasort() instead if you need to keep the array key association intact
          

            Thanks for the help on this, what it has shown me is that I don't understand multi-dimensional arrays in php and I need to study them more (they seem so much more complicated than in other languages)!

            You did however give me the idea of splitting the multi-dimensional array back up into individual arrays, then array_multisort ing them all, and that has done exactly what I was trying to do, so thanks for pointing me in the right direction.

              PHP's "multidimensional arrays" are conceptually much the same as those in, say, Java, C, or JavaScript: they're arrays of arrays. C# has real multidimensional arrays but also supports arrays of arrays. The differences with those languages are the same as for single-dimensional arrays: it's not necessary to specify their size when you declare them and (unlike C) you can't accidentally run off the end.

              The most visible difference compared with those languages is that you can use strings as array indices (you can use the same syntax in JavaScript but it means something different); but that's not really so different from specifying a bunch of enums and using those.

                Write a Reply...