想要在php中添加多维数组

时间:2018-08-17 08:57:25

标签: php arrays multidimensional-array array-sum

我有两个多维数组。如果键相同,那么我想得到总和,我该怎么做。 一个是-

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

4 个答案:

答案 0 :(得分:2)

这是您的代码,

<?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

答案 1 :(得分:0)

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

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

这将返回数组中的交集。因此,您可以确定需要添加哪些键(以您的情况为准,哪些日期)。

答案 2 :(得分:0)

您可以使用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

答案 3 :(得分:0)

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

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

代码:(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,
  ),
)