简体   繁体   English

php $ array [*] [this的总和]排序数组

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

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. 我知道获得总分的唯一方法是array_sum($allplayers[0][2])或类似的方法。 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? 我如何对数组$allplayers排序,以便在循环遍历它以输出结果时将以最高的array_sum开始array_sum下进行?

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 )

You can achive desired result from usort() php built in function. 您可以从内置函数usort()php中获得所需的结果。 which is suggested by @mark baker before that you have to understand the function clearly so some explaination of usort 这是@mark baker建议的,在您必须清楚地了解该功能之前,请对usort进行一些解释

Syntax: 句法:

usort($unsorted_array,callbakc_function)

the callback function will have two arguments which are consequetive elements of the unsorted array. 回调函数将有两个参数,它们是未排序数组的连续元素。 eg 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 例如,第一次将是(0,1),第二(1,2),依此类推,函数应按照以下条件返回0,1或-1

  1. if your comparision of both element on specific condition is equal then 0 如果您在特定条件下两个元素的比较相等,则为0

  2. if your comparision of both element on specific condition is lesser then -1 如果您在特定条件下对这两个元素的比较小于-1

  3. if your comparision of both element on specific condition is equal then 1 如果您在特定条件下两个元素的比较相等,则为1

and php will sort array on this return value eg if your function returns 1 for elements index 0,1 then it will swap the two elements. 并且php将根据此返回值对数组进行排序,例如,如果您的函数为元素索引0,1返回1,则它将交换两个元素。 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. 注意:回调函数的第一个参数将是数组的下一个元素,第二个参数将是该实例的当前元素。 eg at comparing (0,1) index first argument wil be array 1 and 2nd will be array[0]. 例如,在比较(0,1)索引时,第一个参数为数组1 ,第二个参数为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. 现在让我们看一下您的问题,如果在这里使用usort,您将获得每个播放器数组,然后必须计算位于第三个索引元素上的每个播放器分数的总和,并比较这两个总和并返回适当的整数值。 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. 参考:来自PHP文档的usort()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM