想在 php 中添加多维数组

want to add multidimensional array in php

我有两个多维 array.If 键是相同的,那么我想得到总和我该怎么做。 一个是-

Array
(
    [2018-08-02] => Array
        (
            [male] => 1
            [female] => 0
        )



    [2018-08-07] => Array
        (
            [male] => 1
            [female] => 0
        )

   [2018-08-09] => Array
        (
            [male] => 1
            [female] => 5
        )

)

第二个是- 一个是-

Array
(
    [2018-08-02] => Array
        (
            [male] => 3
            [female] => 4
        )



    [2018-08-07] => Array
        (
            [male] => 1
            [female] => 5
        )
[2018-08-06] => Array
        (
            [male] => 2
            [female] => 3
        )


)

所以我的结果是

Array
(
    [2018-08-02] => Array
        (
            [male] => 4
            [female] => 4
        )



    [2018-08-07] => Array
        (
            [male] => 2
            [female] => 5
        )

   [2018-08-09] => Array
        (
            [male] => 1
            [female] => 5
        )
[2018-08-06] => Array
        (
            [male] => 2
            [female] => 3
        )
)

密码是

foreach ($reportlist as $reportlists){ 
    $daterep=$reportlists['act_date']; 
    $arr[$daterep]['male']=$reportlists['male_cnt']; 
    $arr[$daterep]['female']=$reportlists['female_cnt']; 
}

foreach ($ureportlist as $ureportlists){ 
    $daterep=$rueportlists['act_date']; 
    $arr2[$daterep]['male']=$reportlists['male_cnt']; 
    $arr2[$daterep]['female']=$reportlists['female_cnt'];
}

这是您的代码,

<?php
function pr($arr = [])
{
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
}
$arr1 = array
    (
    "2018-08-02" => array
    (
        "male"   => 1,
        "female" => 0,
    ),

    "2018-08-07" => array
    (
        "male"   => 1,
        "female" => 0,
    ),

    "2018-08-09" => array
    (
        "male"   => 1,
        "female" => 5,
    ),

);
$arr2 = array
    (
    "2018-08-02" => array
    (
        "male"   => 3,
        "female" => 4,
    ),

    "2018-08-07" => array
    (
        "male"   => 1,
        "female" => 5,
    ),
    "2018-08-06" => array
    (
        "male"   => 2,
        "female" => 3,
    ),
);

function custom_function($arr){
    $retArr = array();
    foreach ($arr as $child) { // arr1, arr2
        foreach ($child as $key => $value) { // traversing through keys
            foreach($value as $k => $v){
                if (isset($retArr[$key][$k])) { // if key is set then add
                    $retArr[$key][$k] += $v;
                } else {
                    $retArr[$key][$k] = $v; // else initiate
                }

            }
        }
    } 
    return $retArr;
}
$result=custom_function(array($arr1,$arr2));
pr($result);die;

这是你的作品code

您可以使用 array_intersect_key() 来检查是否有匹配的结果来添加它们。 例如,假设您的两个数组是 $array1 和 $array2。然后你可以使用

$result = array_intersect_key($array1, $array2);

这将return 交集在数组中。因此,您可以确定需要添加哪些键(在您的情况下,是哪些日期)。

您可以使用array_merge_recursive连接数组,然后对结果数组中的子数组求和

    $res = array_merge_recursive($arr1, $arr2);
    foreach($res as &$date) {
      foreach($date as &$sex) { 
        $sex = array_sum((array) $sex);
      }
    }
    print_r($res);

demo

您只需迭代第二个数组并确定日期键是否存在于第一个数组中。 isset() 是 运行 检查的最佳/最有效方法。

如果第一个数组中不存在第二个数组中的日期,您可以简单地将行的数据存储到第一个数组中。如果日期已经存在于第一个数组中,那么您将需要(分别)添加男性和女性值。

代码:(Demo)

$array1 = [ 
   '2018-08-02' =>  ['male' => 1, 'female' => 0],
   '2018-08-07' =>  ['male' => 1, 'female' => 0],
   '2018-08-09' =>  ['male' => 1, 'female' => 5]
];
$array2 = [ 
   '2018-08-02' =>  ['male' => 3, 'female' => 4],
   '2018-08-07' =>  ['male' => 1, 'female' => 5],
   '2018-08-06' =>  ['male' => 2, 'female' => 3]
];

foreach ($array2 as $date => $row) {
    if (!isset($array1[$date])) {
        $array1[$date] = $row;                        // store row data with unique date
    } else {
        $array1[$date]['male'] += $row['male'];       // perform addition
        $array1[$date]['female'] += $row['female'];   // perform addition
    }
}
// ksort($array1); if you want to order by date
var_export($array1);

输出:

array (
  '2018-08-02' => 
  array (
    'male' => 4,
    'female' => 4,
  ),
  '2018-08-07' => 
  array (
    'male' => 2,
    'female' => 5,
  ),
  '2018-08-09' => 
  array (
    'male' => 1,
    'female' => 5,
  ),
  '2018-08-06' => 
  array (
    'male' => 2,
    'female' => 3,
  ),
)