-
-
Save k4zuki02h4t4/6ad9fd52b53add2b4d12295f68026133 to your computer and use it in GitHub Desktop.
Sums the values of two associative arrays with the same keys. 同じキーを持つ 2 つの連想配列の値を合計します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Sums the values of two associative arrays with the same keys. | |
* | |
* This function takes two associative arrays with identical keys and | |
* returns a new array where each value is the sum of the corresponding | |
* values from the input arrays. | |
* | |
* @param array $array1 The first associative array. | |
* @param array $array2 The second associative array. | |
* | |
* @return array The resulting associative array with summed values. | |
*/ | |
function array_sum_values($array1, $array2) { | |
return array_reduce( | |
array_keys($array1), | |
function ($carry, $key) use ($array1, $array2) { | |
$carry[$key] = $array1[$key] + $array2[$key]; | |
return $carry; | |
}, | |
[] | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment