want to add multidimensional array in php

101 Views Asked by At

I have two multi dimensional array.If the key is same then i want to get the sum how do i do that. one is-

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
        )

)

2nd one is- one is-

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
        )


)

so my result would be

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

Code is

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

There are 4 best solutions below

0
On BEST ANSWER

Here is your code,

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

Here is your working code

4
On

You can use array_merge_recursive to join arrrays and then sum sub-arrays in the result array

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

demo

0
On

You can use array_intersect_key() to check if there is a matching results to add them. for example, Let's assume your two arrays are $array1 and $array2. Then you can use

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

This will return intersection in an array. So you can identify which keys have (in your case, which dates) need to be add.

0
On

You simply need to iterate the 2nd array and determine whether or not the date keys exist in the 1st array. isset() is the best / most efficient way to run that check.

If a date from the 2nd array does not yet exist in the first, you can simply store the row's data to the 1st array. If the date already exists in the 1st array, then you will need to add (individually) the male and female values.

Code: (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);

Output:

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,
  ),
)