How do I combine these two arrays, so that the keys stay the same, but the values are arithmetically determined?
Note – the keys might not always line up in each array, per my example:
$Array1 = [4 => 100, 5 => 200, 6 => 100, 7 => 400];
$Array2 = [2 => 300, 5 => -100, 16 => -500];
Desired output:
$Array3 = [2 => 300, 4 => 100, 5 => 100, 6 => 100, 7 => 400, 16 => -500];
You can use
array_mapfor this:However this will only work if you have the same keys in both arrays (which, in your example, you don’t).
If this is an issue, the easiest workaround would probably be:
EDIT Just realised the above code is not optimal. Rewriting:
Actually not sure if the overhead of repeated keys is more or less than the overhead of checking uniqueness…