简体   繁体   English

PHP,求和两个数组值

[英]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). 我们可以创建一个包含两个数组的新数组,以稍微缩短我们的代码(请参阅如何将[$first, $second]定义为第一个循环)。

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 您可以使用array_keys从两个数组中获取唯一性,然后遍历一些键

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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