If you got here, you are probably wondering what is the difference between array_replace() andarray_merge().
I’ll start with the syntax:
array_replace( &$array, &$array [, array &$ …]);
array array_replace( $array [, array &$ …]);
And here is the description for each of them (the descriptions are in the same order as written in the syntax section):
array_replace() replaces the values of the first array with the same values from all the following arrays. If a key from the first array exists in the second array, its value will be replaced by the value from the second array. If the key exists in the second array, and not the first, it will be created in the first array. If a key only exists in the first array, it will be left as is. If several arrays are passed for replacement, they will be processed in order, the later arrays overwriting the previous values.
array_merge() merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. 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. Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.
If you still ask yourself what the difference is, continue reading.
The differences are:
- array_replace() replaces the values in the first array with the values from the second one; if the first array doesn’t contain a value that is in the second one, it gets created in the first one, but not vice versa
- array_merge() replaces only the values that have the same string key, the ones with numeric keys get left alone, and both the string keys (that don’t exist in the first array) and the later ones get appended.