简体   繁体   中英

php, sum two array values

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?

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

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

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

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

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

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

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!

$sumArray = [];

foreach($firstArray as $key => $value) {
    $sumArray[$key] = $value + ($secondArray[$key] ?? 0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM