1

I have a multidimensional array which stores information about players in a game. It stores an array for each player, and each player is an array of information. One of the values in each player is an array of scores. I want to output my data to a table in order of highest total score to lowest.

At the moment, the output is in no particular order. The only way I know of to get the total score is array_sum($allplayers[0][2]) or similar. How can I sort the array $allplayers so that when I loop through it to output results it will start with the highest array_sum and work its way down?

Example of array:

//I want to sort $Allplayers by sum of key [3]
$Allplayers ( [0] => Array (
                  [0] => Winning 
                  [1] => 224 
                  [4] => 0 
                  [2] => Array ( [0] => 107 [1] => 114 [3] => 104  ) 
                  [3] => Array ( [0] => 107 [1] => 114 ) )
             [1] => Array ( 
                  [0] => Losing 
                  [1] => 225  
                  [2] => Array ( [0] => 76 )  
                  [3] => Array ( [0] => 76 )  
                  [4] => 1 )
4
  • 2
    Use usort() with a callback that compares the sums Commented Nov 15, 2015 at 13:37
  • @MarkBaker Thanks for the link. I have done this once before but didn't really understand it fully. I now have this line of code usort(array_sum($allplayers[][3]),"cmp"); after the function. what should I put between the first []? That's what I don't get. Thank again Commented Nov 15, 2015 at 13:47
  • Will you please post the array it will clear the question. Commented Nov 15, 2015 at 14:34
  • @siddhesh, Sure, see update in question. Thanks Commented Nov 15, 2015 at 14:51

1 Answer 1

1

You can achive desired result from usort() php built in function. which is suggested by @mark baker before that you have to understand the function clearly so some explaination of usort

Syntax:

usort($unsorted_array,callbakc_function)

the callback function will have two arguments which are consequetive elements of the unsorted array. e.g for first time it will be (0,1) at second (1,2) and so on and function should return the 0,1 or -1 as per following conditions

  1. if your comparision of both element on specific condition is equal then 0

  2. if your comparision of both element on specific condition is lesser then -1

  3. if your comparision of both element on specific condition is equal then 1

and php will sort array on this return value e.g. if your function returns 1 for elements index 0,1 then it will swap the two elements. and at last your array will be sorted using custom condition.

NOTE:callback function first argument will be next element of the array and second will be the current at that instance. e.g at comparing (0,1) index first argument wil be array1 and 2nd will be array[0].

Now let's look at your problem if you use usort here you will get each player array at your function then you have to calculate sum of each player scores which located at 3rd index element and compare both sums and return the appropriate integer value. so you should code this problem like this one

sort($Allplayers,"cmp");
function cmp($a,$b)
{
    $total1 =array_sum($a[3]); 

    $total2 =array_sum($b[3]); 
    if($total1 == $total2)
        return 0;
    return ($total1 > $total2)? -1 :1;
}

REFERENCES: usort() from php documention.

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

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.