What is the difference between array_merge() and array_merge_recursive()?

Introduction to array_merge() and array_merge_recursive()

In PHP, `array_merge()` and `array_merge_recursive()` are two functions used to merge arrays. While they might seem similar, they have different behaviors and use cases. Understanding their differences is crucial for effective array manipulation in PHP.

array_merge()

The `array_merge()` function merges one or more arrays into one. It appends the elements of each array to the end of the previous array. If the arrays have the same string keys, the later value will overwrite the previous value. Numeric keys are re-indexed.

Behavior of array_merge()

When using `array_merge()`, the function combines arrays and returns a new array. The original arrays remain unchanged. If the input arrays have numeric keys, the resulting array will have keys that are re-indexed. If the input arrays have string keys, the values from later arrays will overwrite values from earlier arrays.

Example

Consider the following example:

$array1 = array("a" => "apple", "b" => "banana");
$array2 = array("b" => "blueberry", "c" => "cherry");

$result = array_merge($array1, $array2);
print_r($result);

In this example, the resulting array will have the value "blueberry" for key "b", as it overwrites the earlier value.

array_merge_recursive()

The `array_merge_recursive()` function merges arrays in a recursive manner. When arrays with the same string keys are encountered, it combines the values into an array rather than overwriting them. For numeric keys, the function appends the elements to the end of the resulting array.

Behavior of array_merge_recursive()

With `array_merge_recursive()`, if the arrays have the same string keys, the function merges the values into a new array, preserving all values. For numeric keys, the function combines the elements into a single array.

Example

Consider the following example:

$array1 = array("a" => "apple", "b" => array("x" => "x-ray"));
$array2 = array("b" => array("y" => "yellow"));

$result = array_merge_recursive($array1, $array2);
print_r($result);

In this example, the resulting array will merge the values under key "b" into an array containing both "x-ray" and "yellow".

Key Differences

  • Handling of Duplicate Keys: `array_merge()` overwrites values for duplicate string keys, while `array_merge_recursive()` combines them into arrays.
  • Numeric Keys: Both functions re-index numeric keys, but `array_merge_recursive()` appends elements to the end.
  • Use Cases: Use `array_merge()` when you want to overwrite values, and `array_merge_recursive()` when you want to preserve and combine values.

Conclusion

Choosing between `array_merge()` and `array_merge_recursive()` depends on how you want to handle duplicate keys and the structure of your arrays. Use `array_merge()` for straightforward merging where you don"t need to preserve duplicate values, and `array_merge_recursive()` when you need to retain and combine all values from the input arrays.

18 Aug 2024   |    12

article by ~ raman gulati

Top related questions

Related queries

Latest questions