How to use PHP to calculate the sum of elements in two arrays?

You can use a loop to iterate through two arrays and add the elements at corresponding positions, as shown below:

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

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

print_r($result);

The above code will produce:

Array
(
    [0] => 7
    [1] => 9
    [2] => 11
    [3] => 13
    [4] => 15
)
bannerAds