2

I have two array

first array:

Array ( 
    [01-1970] => 0.00 
    [03-2019] => 4350.00 
    [05-2019] => 150.00 
    [06-2019] => 50.00 
)

second array:

Array ( 
    [03-2019] => 0.00
    [04-2019] => 0.00 
    [06-2019] => 34.83 
)

My expected sum result is:

Array ( 
    [01-1970] => 0.00 
    [03-2019] => 4350 
    [04-2019] => 0.00  
    [05-2019] => 150.00 
    [06-2019] => 84.83
)

How can achieve this?

Qirel
  • 25,449
  • 7
  • 45
  • 62
askerman
  • 51
  • 1
  • 8

8 Answers8

3

Your best bet is to loop the arrays individually, and sum up the values into a resulting array as you go. We can create a new array that contains the two arrays them to shorten our code a bit (see how we define [$first, $second] as the first loop).

This removes any problems with mixed lengths, and keeps all the keys and values in the array intact.

$result = [];
// Loop over our two arrays, here called $first and $second
foreach ([$first, $second] as $a) {
    // Loop over the values in each array
    foreach ($a as $k=>$v) {
        // If the index is new to the $result array, define it to be zero (to avoid undefined index notices) 
        if (!isset($result[$k]))
            $result[$k] = 0;

        // Sum up the value!
        $result[$k] += $v;
    }
}
print_r($result);
Qirel
  • 25,449
  • 7
  • 45
  • 62
3

You can use array_keys to get the unique from both of the array and then loop through keys to some them

$r = [];
$keys = array_keys($a1+$a2);
foreach($keys as $v){
  $r[$v] = (empty($a1[$v]) ? 0 : $a1[$v]) + (empty($a2[$v]) ? 0 : $a2[$v]);
}

Working DEMO

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
2

You can make use of a function I made:

<?php
    function array_sum_multi($arrayOne, $arrayTwo)
    {
        # get rid of keys
        $valuesOne = array_values($arrayOne);
        $valuesTwo = array_values($arrayTwo);

        //create return array
        $output = [];

        # loop that shizzle
        for ($i = 0; $i < count($valuesOne); $i++)
        {
            $output[$i] = $valuesOne[$i] + (!empty($valuesTwo[$i]) ? $valuesTwo[$i] : 0);
        }

        return $output;
    }

    $result = array_sum_multi([0.00, 4350.00, 150.00, 50.00], [0.00, 0.00, 34.83]);

    # then for your keys:
    $result = array_combine(array_keys($yourFirstArray), $result);

    echo '<pre>'. print_r($result, 1) .'</pre>';
treyBake
  • 6,440
  • 6
  • 26
  • 57
1
$result = $first_array; // just copy array into result

// scan second array
foreach ($second_array as $k => $v) {
   // if key already exists, then add, else just set
   $result[$k] = isset($result[$k]) ? ($result[$k] + $v) : $v;
}

// done
print_r($result);
AterLux
  • 4,566
  • 2
  • 10
  • 13
  • You probably ment to print `$result`, and not `$second_array`? :-) Of all the "fixed two arrays" answers, this is the most elegant way, imo (my answer is a tad more dynamic and can handle more than 2 arrays, so that's the difference there). +1 – Qirel Jul 08 '19 at 11:24
  • @Qirel yes, you're right. That should be `$result`. Thanks for pointing it out. I edited the answer. This one can handle more than 2 arrays too. All what needed is to repeat `foreach` part for each next array. The result will be the sum of all of them – AterLux Jul 08 '19 at 11:43
0

An easy way to implement it would be to loop through each array, and add it to a common array with the same key.

Looping through only one array would result in a lack of a few elements if the first array is smaller than the second one or if some element from the second array are not present in the first one.

So let's just loop through both of them and add it to sum.

$sum = [];

foreach($firstArray as $key => $value){
  $sum[$key] = $value + (isset($sum[$key]) ? $sum[$key] : 0.0);
}
foreach($secondArray as $key => $value){
  $sum[$key] = $value + (isset($sum[$key]) ? $sum[$key] : 0.0);
}

print_r($sum);
Dylan KAS
  • 4,840
  • 2
  • 15
  • 33
0

Try this simple method thank you,

$sum = [];
foreach($firstArray as $key => $value){
    if(array_key_exists($key,$secondArray)){
        $newArray = [$key=>$value+$secondArray[$key]]; 
        $sum = array_merge($sum,$newArray);
    }else{
        $newArray = [$key=>$value]; 
        $sum = array_merge($sum,$newArray);
    }
}

//your final required result
var_dump($sum);
-1

Try this,

$a1 = array (
        '01-1970' => 0.00,
        '03-2019' => 4350.00,
        '05-2019' => 150.00,
        '06-2019' => 50.00
    );

$a2 = array (
        '03-2019' => 0.00,
        '04-2019' => 0.00,
        '06-2019' => 34.83
    );

$sums = array();
foreach (array_keys($a1 + $a2) as $key) {
    $sums[$key] = @($a1[$key] + $a2[$key]);
}

echo "<pre>";
print_r($sums);

Here is some other solution you can use.

Cheer!

Geee
  • 2,217
  • 15
  • 30
-2
$sumArray = [];

foreach($firstArray as $key => $value) {
    $sumArray[$key] = $value + ($secondArray[$key] ?? 0);
}
Mike Foxtech
  • 1,633
  • 1
  • 6
  • 7
  • 3
    answers should have explanations so the OP and future visitors can learn – treyBake Jul 08 '19 at 10:07
  • It is a shame that the best / most modern snippet on the page has no educational explanation. ...well, I probably wouldn't populate a third array. – mickmackusa Mar 02 '23 at 11:51