phparrayssummerging-data

Merge row data from two 2d arrays of identical size/structure by first level index and sum 2nd column values


I have two array with same keys this two array contain month wise data of my table. i want sum of this values and return same keys sum of values in one other array

Here is my two array

array1

Array ( [0] => Array ( [0] => Jan [1] => 0 ) 
        [1] => Array ( [0] => Feb [1] => 22 ) 
        [2] => Array ( [0] => Mar [1] => 0 ) 
        [3] => Array ( [0] => Apr [1] => 9 ) 
        [4] => Array ( [0] => May [1] => 1 ) 
        [5] => Array ( [0] => Jun [1] => 0 ) 
        [6] => Array ( [0] => Jul [1] => 0 ) 
        [7] => Array ( [0] => Aug [1] => 0 ) 
        [8] => Array ( [0] => Sep [1] => 0 ) 
        [9] => Array ( [0] => Oct [1] => 0 ) 
        [10] => Array ( [0] => Nov [1] => 0 ) 
        [11] => Array ( [0] => Dec [1] => 0 ) 
    )

array 2:

Array ( [0] => Array ( [0] => Jan [1] => 0 ) 
        [1] => Array ( [0] => Feb [1] => 0 ) 
        [2] => Array ( [0] => Mar [1] => 18 ) 
        [3] => Array ( [0] => Apr [1] => 1 ) 
        [4] => Array ( [0] => May [1] => 1 ) 
        [5] => Array ( [0] => Jun [1] => 0 ) 
        [6] => Array ( [0] => Jul [1] => 0 ) 
        [7] => Array ( [0] => Aug [1] => 0 ) 
        [8] => Array ( [0] => Sep [1] => 0 ) 
        [9] => Array ( [0] => Oct [1] => 0 ) 
        [10] => Array ( [0] => Nov [1] => 0 ) 
        [11] => Array ( [0] => Dec [1] => 0 ) 
    )

i also tried using this code

function sum_arrays($array1, $array2) {
    $array = array();
    foreach($array1 as $index => $value) {
        $array[$index] = isset($array2[$index]) ? $array2[$index] + $value : $value;
    }
    return $array;
}

i want result like below

Array ( [0] => Array ( [0] => Jan [1] => 0 ) 
        [1] => Array ( [0] => Feb [1] => 22 ) 
        [2] => Array ( [0] => Mar [1] => 18 ) 
        [3] => Array ( [0] => Apr [1] => 10 ) 
        [4] => Array ( [0] => May [1] => 2 ) 
        [5] => Array ( [0] => Jun [1] => 0 ) 
        [6] => Array ( [0] => Jul [1] => 0 ) 
        [7] => Array ( [0] => Aug [1] => 0 ) 
        [8] => Array ( [0] => Sep [1] => 0 ) 
        [9] => Array ( [0] => Oct [1] => 0 ) 
        [10] => Array ( [0] => Nov [1] => 0 ) 
        [11] => Array ( [0] => Dec [1] => 0 ) 
    )

Solution

  • array_map approach (if 2 arrays have month items in same order):

    // shortened array samples
    $arr1 = [
        ["Jan", 0],
        ["Feb", 22],
        ["Mar", 0]
    ];
    
    $arr2 = [
        ["Jan", 0],
        ["Feb", 0],
        ["Mar", 18]
    ];
    
    $result = array_map(function($a, $b){
        return [$a[0], $a[1] + $b[1]];
    }, $arr1, $arr2);
    
    print_r($result);
    

    The output:

    Array
    (
        [0] => Array
            (
                [0] => Jan
                [1] => 0
            )
    
        [1] => Array
            (
                [0] => Feb
                [1] => 22
            )
    
        [2] => Array
            (
                [0] => Mar
                [1] => 18
            )
    )