код
<?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
(
)