23

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 :

  1. If the arrays contain numeric keys, the later value will not overwrite the original value in array_merge(), which will be done in array_replace().
  2. 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 with array_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?

luchaninov
  • 6,792
  • 6
  • 60
  • 75
  • @zpr Try what? If you see the referred question and answers, the solution works with both functions. But I am just trying to know, whether there can be other cases, when these two functions will not give the same result for associative arrays. – Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ Dec 19 '15 at 05:42

2 Answers2

28

Jarek gave a nice explanation in his article here:

https://softonsofa.com/php-array_merge-vs-array_replace-vs-plus-aka-union/

He also adds in the use of the + operator with arrays for comparison.

Graphic showing the difference

Gleb Kemarsky
  • 10,160
  • 7
  • 43
  • 68
Will
  • 464
  • 5
  • 7
  • 4
    Try to not use only links as answer. It's good practice to copy valuable information from linked sources to keep them in case the link stopped working – zajonc Jul 18 '16 at 13:52
20

For arrays with string keys, yes these are equivalent, as you mentioned. If you have numeric keys, array_merge() will append them as required, and even re-order them if necessary, whereas array_replace() will overwrite the original values.

For example,

$a = array('a' => 'hello', 'b' => 'world');
$b = array('a' => 'person', 'b' => 'thing', 'c'=>'other', '15'=>'x');

print_r(array_merge($a, $b));
/*Array
(
    [a] => person
    [b] => thing
    [c] => other
    [0] => x
)*/

print_r(array_replace($a, $b));
/*Array
(
    [a] => person
    [b] => thing
    [c] => other
    [15] => x
)*/

As you can see, array_merge has re-indexed the numeric keys of the array, and both of them simply update string keys.

However, when you have numeric keys, array_merge() will simply not care about keys, and add everything in the order it sees, deleting nothing, whereas array_replace() will, as the name suggests, replace keys with similar (numeric) indices:

<?php
$a = array('0'=>'a', '1'=>'c');
$b = array('0'=>'b');

print_r(array_merge($a, $b));
/*Array
(
  [0] => a
  [1] => c
  [2] => b
)*/

print_r(array_replace($a, $b));
/*Array
(
  [0] => b
  [1] => c
)*/
zpr
  • 2,886
  • 1
  • 18
  • 21
  • It should be noted that if the arrays share a numeric key there is a significant difference. Merge will preserve the original contents whereas replace is destructive. – tbernard Dec 19 '15 at 06:02
  • @tbernard What do you mean? How it impacts the contents (not only the keys)? – Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ Dec 19 '15 at 06:12
  • 1
    @TareqMahmood What I mean is this. Given $a = { 0 => 'hello' } and $b = { 0 => 'goodbye' } then array_merge($a,$b) = { 0 => 'hello', 1 => 'goodbye' } whereas array_replace($a,$b) = { 0 => 'goodbye' }. When the keys are numeric and not associative then replace literally replaces. The two functions **only** behave the same for associative keys or keys that are not found in both input arrays. – tbernard Dec 19 '15 at 06:20
  • Also, based on the manual, array_replace could return NULL on error. (I wonder what kind of error can occur in array_replace but not in array_merge.) – user1768761 Feb 08 '21 at 10:41