如何将PHP中单个数组中的各个索引求和?

时间:2019-06-02 01:17:03

标签: php sum

无论我怎么看docs,我都无法理解将两个数组的各个索引加在一起(+)。有人可以解释一下我如何实现以下结果吗?

样本数组:

array1 = [1,0,2,3,4];
array2 = [3,5,6,7,8];

预期输出:

[4, 5, 8, 10, 12]

我尝试过

$arrSum = $array1 + $array2;

但是,这不是我想要的结果。

$arrSum Array
(
    [0] => 1
    [1] => 0
    [2] => 2
    [3] => 3
    [4] => 4
)

3 个答案:

答案 0 :(得分:2)

  

代码

<?php

// Sample array: 
$array1 = [1,0,2,3,4];
$array2 = [3,5,6,7,8];

// Expected Output: 
// [4, 5, 8, 10, 12]

function addArrays(array $array1, array $array2) : array
{
    $result = [];
    $counter = 0;

    //iterate over all values from array 1 
    foreach($array1 as $key => $value) {

        //if in array 2 exists a value at the same key
        //as in array 1 that we iterate
        if (isset($array2[$key])) {
            //the sum is addition of the value
            //of array 1 and value of array 2 under
            //the same key
            $singleSum = $value + $array2[$key];

        } else {
            //array 2 does not have value
            //at the same key so the sum
            //is just a value of array 1
            $singleSum = $value;
        }

        //store sum in result array
        $result[$key] = $singleSum;

        //increment counter of processed
        //values in array 1
        $counter++;
    }

    $array2Count = count($array2);

    //if array 2 has more values than array 1
    if ($array2Count > $counter) {

        //there are some remaining values
        //in array 2 that havent been put
        //into result

        //start from value at position that you end
        //last time and iterate till the last
        //value of array 2
        for ($i = $counter; $i < $array2Count; $i++) {

            //just put that value to result array
            $result[] = $array2[$i];
        }
    }

    return $result;    
}

echo "\n--- exammple 1 ---\n";

echo "\n--- input array1 ---\n";
$array1 = [1,0,2,3,4];
print_r($array1);

echo "\n--- input array2 ---\n";
$array2 = [3,5,6,7,8];
print_r($array2);

echo "\n--- result ---\n";
$result = addArrays($array1, $array2);
print_r($result);

echo "\n--- exammple 2 ---\n";

echo "\n--- input array1 ---\n";
$array1 = [1,0,2];
print_r($array1);

echo "\n--- input array2 ---\n";
$array2 = [3,5,6,7,8];
print_r($array2);

echo "\n--- result ---\n";
$result = addArrays($array1, $array2);
print_r($result);

echo "\n--- exammple 3 ---\n";

echo "\n--- input array1 ---\n";
$array1 = [1,0,2];
print_r($array1);

echo "\n--- input array2 ---\n";
$array2 = [3];
print_r($array2);

echo "\n--- result ---\n";
$result = addArrays($array1, $array2);
print_r($result);

echo "\n--- exammple 4 ---\n";

echo "\n--- input array1 ---\n";
$array1 = [1,0,2];
print_r($array1);

echo "\n--- input array2 ---\n";
$array2 = [];
print_r($array2);

echo "\n--- result ---\n";
$result = addArrays($array1, $array2);
print_r($result);

echo "\n--- exammple 5 ---\n";
echo "\n--- input array1 ---\n";
$array1 = [];
print_r($array1);

echo "\n--- input array2 ---\n";
$array2 = [3,5];
print_r($array2);

echo "\n--- result ---\n";
$result = addArrays($array1, $array2);
print_r($result);


echo "\n--- exammple 6 ---\n";
echo "\n--- input array1 ---\n";
$array1 = [];
print_r($array1);

echo "\n--- input array2 ---\n";
$array2 = [];
print_r($array2);

echo "\n--- result ---\n";
$result = addArrays($array1, $array2);
print_r($result);
  

结果

--- exammple 1 ---

--- input array1 ---
Array
(
    [0] => 1
    [1] => 0
    [2] => 2
    [3] => 3
    [4] => 4
)

--- input array2 ---
Array
(
    [0] => 3
    [1] => 5
    [2] => 6
    [3] => 7
    [4] => 8
)

--- result ---
Array
(
    [0] => 4
    [1] => 5
    [2] => 8
    [3] => 10
    [4] => 12
)

--- exammple 2 ---

--- input array1 ---
Array
(
    [0] => 1
    [1] => 0
    [2] => 2
)

--- input array2 ---
Array
(
    [0] => 3
    [1] => 5
    [2] => 6
    [3] => 7
    [4] => 8
)

--- result ---
Array
(
    [0] => 4
    [1] => 5
    [2] => 8
    [3] => 7
    [4] => 8
)

--- exammple 3 ---

--- input array1 ---
Array
(
    [0] => 1
    [1] => 0
    [2] => 2
)

--- input array2 ---
Array
(
    [0] => 3
)

--- result ---
Array
(
    [0] => 4
    [1] => 0
    [2] => 2
)

--- exammple 4 ---

--- input array1 ---
Array
(
    [0] => 1
    [1] => 0
    [2] => 2
)

--- input array2 ---
Array
(
)

--- result ---
Array
(
    [0] => 1
    [1] => 0
    [2] => 2
)

--- exammple 5 ---

--- input array1 ---
Array
(
)

--- input array2 ---
Array
(
    [0] => 3
    [1] => 5
)

--- result ---
Array
(
    [0] => 3
    [1] => 5
)

--- exammple 6 ---

--- input array1 ---
Array
(
)

--- input array2 ---
Array
(
)

--- result ---
Array
(
)

答案 1 :(得分:2)

$array1 = [1,0,2,3,4];
$array2 = [3,5,6,7,8];

function sum_values($a, $b) {
    return $a+$b;
}

$output = array_map('sum_values', $array1, $array2);

如果数组大小不同,则会将缺少的值视为0。因此,如果您有以下数组:

$array1 = [1,2,3,4,5];
$array2 = [1,2];

输出为:

$output = [2,4,3,4,5];

答案 2 :(得分:0)

由于我们拥有大小相等的数组,因此我们只需编写一个for循环并将它们加起来即可:

$array1 = [1, 0, 2, 3, 4];
$array2 = [3, 5, 6, 7, 8];

$array_sum = [];

if (sizeof($array1) != sizeof($array2)) {
    echo "Our arrays do not have equal length. ";
}

for ($i = 0; $i < sizeof($array1); $i++) {
    $array_sum[$i] = $array1[$i] + $array2[$i];
}

var_dump($array_sum);