I am trying to figure out the differences between array_replace() and array_merge(). The question actually came to my mind after this post : PHP array_merge empty values always less prioritar, where the problem actually can be solved with any of these two functions. So, I was trying to find out in which cases we should use array_replace instead of array_merge and vice versa.
After reading the php documentation for both functions, I find these two differences :
- If the arrays contain numeric keys, the later value will not overwrite the original value in
array_merge()
, which will be done inarray_replace()
. - In
array_merge()
, values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array, which shouldn't happen witharray_replace()
.
Since the differences are only related to numeric keys, can we safely say that, functions array_replace()
and array_merge()
are exactly equivalent when we are dealing with associative arrays? Or is there any other difference which I am missing?