I have two associative arrays that can look as follows:
$prod1 = (
[name] = 'John',
[address] = 'Milky way',
[city] = 'Miami',
);
$prod2 = (
[address] = 'Not so milky way'
);
$prod1 has always got the same kind of elements (name, address, city), but $prod2 could contain of either all of the elements or some of them
What I want is to replace the value in the elements in $prod1 that matches with the elements in $prod2. In the example above $prod2 contains of address, and therefore the address in $prod1 should be replaced with the address in $prod2 (the rest of the elements should of course stay as is).
How can this be accomplished?
The
array_mergefunction will do this for you:From the documentation:
Alternatively, you can use the
+operator, which will give you the same result: