简体   繁体   English

对这个数组进行分组和求和

[英]Group and Sum this Array

I have an array that's output is this:我有一个数组 output 是这样的:

Array ( [winners] => 
Array ( [0] => Gold Member 
        [1] => CROTCH SNIFFER 
        [2] => TEAM #1 )         
        [prizeTotal] => 20 )

Array ( [winners] => 
Array ( [0] => TEAM #1  
        [1] => CROTCH SNIFFER )
        [prizeTotal] => 60 ) 

Array ( [winners] => 
Array ( [0] => Gold Member 
        [1] => TEAM #1 ) 
        [prizeTotal] => 30 )

Array ( [winners] => 
Array ( [0] => TEAM #1 
        [1] => TEAM #2
        [2] => SCREW-NUT-BOLT )
        [prizeTotal] => 90 ) 
  1. Please forgive the names...it's not my DB.请原谅这些名字……这不是我的数据库。
  2. I can not change the way the array is show here.我无法更改数组在此处的显示方式。

With that being said how can I group and sum?话虽如此,我该如何分组和求和? 1. For each winners array there is a prizeTotal below the team names. 1. 对于每个获胜者数组,团队名称下方都有一个 PrizeTotal。 That prize total should be the value of each teamName above it.该奖金总额应该是其上方每个团队名称的值。

Example例子

Array ( [winners] => 
Array ( [0] => Gold Member 
        [1] => CROTCH SNIFFER 
        [2] => TEAM #1 )         
        [prizeTotal] => 20 )
Gold Member should have 20
CROTCH SNIFFER should have 20
TEAM #1 should have 20 AND

Array ( [winners] => 
Array ( [0] => TEAM #1  
        [1] => CROTCH SNIFFER )
        [prizeTotal] => 60 ) 
TEAM #1 should have 60
CROTCH SNIFFER sould have 60....etc....
  1. Then I want to group by team name and sum so that I can display...然后我想按团队名称和总和分组,以便我可以显示......

    CROTCH SNIFFER = 80 Gold Member = 50 TEAM #1 = 200 Team #2 = 90.裆部嗅探器 = 80 黄金会员 = 50 团队 #1 = 200 团队 #2 = 90。

Thanks in advance...提前致谢...

Assuming you can collect all your arrays into one ie :假设您可以将所有 arrays 收集到一个中,即:

$arrays = array($array1,$array2,....,$arrayn);

then然后

$grouping = array();

foreach($arrays AS $array)
{
   foreach($array['winners'] AS $k=>$v)
   {
      //check if the array key is a number, will contain a team, and if that
      //them is not alreay listed
      if(is_numeric($k) && !array_key_exists($v,$grouping))
         $grouping[$v] = 0;
      //sum the prize to the team's sum
      $grouping[$v] += intval($array['winners']['prizeTotal']);
   }
}
//the following is just for debugging
print_r($grouping);

this should produce something like:这应该产生类似的东西:

$grouping[TEAM #1] = 200
$grouping[TEAM #2] = 90
$grouping[CROTCH SNIFFER] = 200
...

I have made a function that imitates mysql SUM() and GROUP BY .我制作了一个模仿 mysql SUM()GROUP BY的 function 。 I hope it will fit your needs:我希望它能满足您的需求:

           $in_a = array(
                array("a" => 0,"b"=>0,"s"=> 1),
                array("a" => 0,"b"=>0,"s"=> 2),
                array("a" => 1,"b"=>1,"s"=> 1),
                array("a" => 0,"b"=>1,"s"=> 1),
                array("a" => 0,"b"=>1,"s"=> 1),
                array("a" => 1,"b"=>0,"s"=> 1),         
                array("a" => 0,"b"=>1,"s"=> 1),         
                array("a" => 1,"b"=>1,"s"=> 1),         
                array("a" => 1,"b"=>0,"s"=> 1),         
            );//input array exaple
            $group_by_a = array("a","b");//input array will be grouped by these
            $sum_a = array("s"); //'s' values of input will be summed
            $out_a = array(); //this is the output array

            foreach($in_a as $in_i => $in)
            {
                $add = false;
                foreach($out_a as $out_i => $out)
                {
                    $add = true;
                    foreach($group_by_a as $group_by)
                        if($in[$group_by] != $out[$group_by])
                        {
                            $add = false; 
                            break;
                        }                   
                    if($add)
                    {
                        foreach($sum_a as $sum)
                            $out_a[$out_i][$sum] += $in[$sum];  
                        break;
                    }
                }
                if(!$add)
                {
                    foreach($group_by_a as $group_by)
                        $out_a[$in_i][$group_by] = $in[$group_by];
                    foreach($sum_a as $sum)
                        $out_a[$in_i][$sum] = $in[$sum];                    
                }                   
            }

the result:结果:

Array
(
    [0] => Array
        (
            [a] => 0
            [b] => 0
            [s] => 3
        )

    [2] => Array
        (
            [a] => 1
            [b] => 1
            [s] => 2
        )

    [3] => Array
        (
            [a] => 0
            [b] => 1
            [s] => 3
        )

    [5] => Array
        (
            [a] => 1
            [b] => 0
            [s] => 2
        )

)

You need to make a new array to hold your results, then for each of your existing arrays, check if you're already processed one for that team您需要创建一个新数组来保存您的结果,然后对于您现有的每个 arrays,检查您是否已经为该团队处理了一个

If you have, then simply add the prizeTotal to the prizeTotal stored in your new array's entry for that team.如果有,那么只需将prizeTotal 添加到存储在该团队的新数组条目中的prizeTotal 中。

If not, simply add the team's entry to your new array.如果没有,只需将团队的条目添加到您的新数组中。

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

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