1

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 ) 
    )
Barmar
  • 741,623
  • 53
  • 500
  • 612

2 Answers2

0

Using The foreach Loop:

$sum = 0;

foreach($items as $item) {
    $sum += $item['qty'];
}

echo $sum;

Using array_map():

echo array_sum(array_map(
    function($item) {
        return $item['qty'];
    }, $items)
);

Using array_reduce():

echo array_reduce($items, function($carry, $item) {
    $carry += $item['qty'];
    return $carry;
});
  • my friends please see my question i have two array and i want result like in my question – Kiran Suthar May 14 '17 at 17:43
  • I just showed you the method you may try. By the way, dont forget to check this: http://stackoverflow.com/questions/3286749/array-sum-of-value-based-on-same-key and http://stackoverflow.com/questions/14195916/associative-array-sum-values-of-the-same-key – shameemreza May 14 '17 at 17:51
  • There's no `qty` key in his arrays. – Barmar May 14 '17 at 18:20
0

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
        )
)
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105