php sort array by $array[*][sum of this]

时光怂恿深爱的人放手 提交于 2021-01-28 23:12:36

问题


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 )

回答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.



来源:https://stackoverflow.com/questions/33720295/php-sort-array-by-arraysum-of-this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!