Archive for July 2010
PHP: array_merge versus array union operator (+)
It comes that even experienced PHP developers don’t remember what is the difference between running array_merge((array)$a1, (array)$a2) and (array)$a1+(array)$a2. The answer is given @ php.net, no need to guess and argue.
Here is the difference:
Array Union: The + operator appends elements of remaining keys from the right handed array to the left handed, whereas duplicated keys are NOT overwritten.
array_merge(): If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended. If all of the arrays contain only numeric keys, the resulting array is given incrementing keys starting from zero.