使用一个多维关联数组对另一个多维数组进行计数或求和

时间:2019-05-02 12:52:35

标签: php multidimensional-array

我有两个具有相同键的多维关联数组。我需要根据另一个数组的要求计算/求和一个数组中的特定值。

我尝试了嵌套的foreach循环,但无法获取请求的结果。我已经尽我所能搜索了互联网,但没有成功。

这是我的代码无法按要求工作:

foreach ($b as $key => $value) {
  foreach ($a as $skey => $svalue) {
    if ($key == $skey) {
      foreach ($svalue[Season] as $ssvalue) {
        if ($value[Appearance] == $ssvalue) {
          $c[$key]['SeasonAppearances'][] = count($value[Appearance]);
        }
      }
    }
  }
}

第一个数组(提取)/ $ a

$a = Array ( 
        [Paul] => Array ( 
                [Season] => Array ( 
                        [0] => 2014 
                        [1] => 2015 
                        [2] => 2016 
                        ) 
                ) 
        [John] => Array ( 
                [Season] => Array ( 
                        [0] => 2012 
                        ) 
                ) 
    )

第二个数组(提取)/ $ b

$b = Array ( 
        [Paul] => Array ( 
                [Appearance] => Array ( 
                            [0] => 2014 
                            [1] => 2014 
                            [2] => 2014 
                            [3] => 2014 
                            [4] => 2014 
                            [5] => 2014 
                            [6] => 2014 
                            [7] => 2014 
                            [8] => 2015 
                            [9] => 2015 
                            [10] => 2015 
                            ) 
                ) 
        [John] => Array ( 
                [Appearance] => Array ( 
                            [0] => 2012 
                            [1] => 2012 
                            [2] => 2012 
                            [3] => 2012 
                            ) 
                ) 
        )

请求的结果数组/ $ c

$c = Array ( 
        [Paul] => Array ( 
            [SeasonCounts] => Array ( 
                        [0] => 8 
                        [1] => 3 
                        [2] => 0 
                        ) 
            ) 
        [John] => Array ( 
            [SeasonCounts] => Array ( 
                        [0] => 4 
                        ) 
            ) 
    )

我想计算$ a中每个季节每个人与每个人匹配的出场次数,并创建结果数组。请注意,保罗在2016年没有出场。

2 个答案:

答案 0 :(得分:3)

一种无条件的功能方法:(Demo

$season_data = [
    'Paul' => [
        'Season' => [2014, 2015, 2016]
    ],
    'John' => [
        'Season' => [2012]
    ]
];

$appearance_data = [
    'Paul' => [ 
        'Appearance'=> [
            2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2015, 2015, 2015 
        ]
    ],
    'John' => [
        'Appearance' => [
            2012, 2012, 2012, 2012 
        ]
    ]
];

foreach ($season_data as $person => $data) {
    $defaults = array_fill_keys($data['Season'], 0);
    $result[$person]['SeasonCounts'] = array_replace(
        $defaults,
        array_intersect_key(
            array_count_values($appearance_data[$person]['Appearance']),
            $defaults
        )
    );
}
var_export($result);

输出:

array (
  'Paul' => 
  array (
    'SeasonCounts' => 
    array (
      2014 => 8,
      2015 => 3,
      2016 => 0,
    ),
  ),
  'John' => 
  array (
    'SeasonCounts' => 
    array (
      2012 => 4,
    ),
  ),
)

使用此任务的功能可以阐明正在执行的任务-使将来的开发人员可以轻松理解代码。

  • 对于每个人,我将期望的年份临时存储为零。
  • 然后我打电话给array_count_values(),以快速生成外观年及其计数的数组(这是该功能诞生的确切任务)。
  • 然后过滤数组以仅保留使用array_intersect_key()在人的季节数组中指定的年份。
  • 最后,通过array_replace()用新的相应计数值替换任何零。

作为上述内容的略微修改,您可以过滤{em>之前的计数,例如:Demo

每个人只需执行四个功能清晰,可读的函数调用即可获得所需的确切结果。

我的结果数组是用关联的SeasonCounts子数组设计的,因为数据实际上是关联的。用索引计数存储输出会松开关系-使用我的方法可以避免这种损失。


或者,您可以为每个人的每个Season重复进行一次filter&count调用。 (Same result

foreach ($season_data as $person => $data) {
    foreach ($data['Season'] as $year) {
        $result[$person]['SeasonCounts'][$year] = sizeof(
            array_filter(
                $appearance_data[ $person]['Appearance'],
                function($v) use ($year) {
                    return $v == $year;
                })
            );
    }
}

最后是一种无功能的条件递增方法。 (Same result

foreach ($season_data as $person => $data) {
    foreach ($data['Season'] as $year) {
        $result[$person]['SeasonCounts'][$year] = 0;
        foreach ($appearance_data[$person]['Appearance'] as $appearance) {
            if ($appearance == $year) {
                ++$result[$person]['SeasonCounts'][$year];
            }
        }
    }
}

答案 1 :(得分:0)

这应该像您想要的那样计数

$c = array();
$helper = array();
foreach ($b as $key => $value) {

    $c[$key] = array();
    $c[$key]["SeasonCount"] = array();
    $helper[$key] = array();
    foreach ($value["Appearance"] as $key1 => $value1) {

        $position = array_search($value1, $helper[$key]);
        if (false !== $position) {
            $c[$key]["SeasonCount"][$position]++;
        } else {
            $helper[$key][] = $value1;
            $c[$key]["SeasonCount"][] = 1;
        }

    }}