php 将3d数组转换为唯一值的关联数组,并对相关值求和

mwyxok5s  于 2023-03-16  发布在  PHP
关注(0)|答案(4)|浏览(103)

我需要扁平化一个多维数组,以形成一个唯一值及其总和的关联数组。
我的样品阵列:

[
    [
        'winners' => [
            'Gold Member',
            'CROTCH SNIFFER',
            'TEAM #1'
        ],     
        'prizeTotal' => 20
    ],
    [
        'winners' => [
            'TEAM #1',
            'CROTCH SNIFFER'
        ],     
        'prizeTotal' => 60
    ],
    [
        'winners' => [
            'Gold Member',
            'TEAM #1'
        ],     
        'prizeTotal' => 30
    ],
    [
        'winners' => [
            'TEAM #1',
            'TEAM #2',
            'SCREW-NUT-BOLT'
        ],     
        'prizeTotal' => 90
    ]
]

请原谅这些名字...这不是我的DB。
在每个数据集中,prizeTotal被授予winners子数组中的每个值。
如何对winners值进行分组,并对它们各自的prizeTotal值求和?
预期结果:

array (
  'Gold Member' => 50,
  'CROTCH SNIFFER' => 80,
  'TEAM #1' => 200,
  'TEAM #2' => 90,
  'SCREW-NUT-BOLT' => 90,
)
tp5buhyn

tp5buhyn1#

假设您可以将所有数组收集到一个数组中,即:

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

那么

$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);

这将产生如下结果:

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

o3imoua42#

您需要创建一个新数组来保存结果,然后对于每个现有数组,检查是否已经为该团队处理了一个数组
如果有,那么只需将prizeTotal添加到存储在新数组条目中的该团队的prizeTotal。
如果没有,只需将团队的条目添加到新数组中。

vltsax25

vltsax253#

要生成唯一团队及其累积得分的关联数组:
1.使用循环访问每个数据子集
1.使用extract()可以方便地将赢家和prizeTotal数据作为单个变量访问。
1.循环赢家元素
1.将赢家声明为结果数组中的键,其值应为prizeTotal加上缓存值(如果没有缓存值,则为零)。
代码:(Demo

$result = [];
foreach ($array as $set) {
    extract($set);
    foreach ($winners as $winner) {
        $result[$winner] = ($result[$winner] ?? 0) + $prizeTotal;
    }
}
var_export($result);

“数组解构”也可以创建单独的变量,而不是调用extract()

$result = [];
foreach ($array as ['winners' => $winners, 'prizeTotal' => $prizeTotal]) {
    foreach ($winners as $winner) {
        $result[$winner] = ($result[$winner] ?? 0) + $prizeTotal;
    }
}
var_export($result);
ffscu2ro

ffscu2ro4#

我模仿mysql SUM()GROUP BY做了一个函数,希望能满足你的需求:

$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];                    
                }                   
            }

结果是:

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
        )

)

相关问题