ZetCode

PHP array_merge_recursive Function

last modified March 13, 2025

The PHP array_merge_recursive function merges two or more arrays recursively. Unlike array_merge, it handles duplicate keys by creating arrays of values.

Basic Definition

The array_merge_recursive function combines arrays while preserving keys. When keys match, values are merged into an array rather than overwritten.

Syntax: array_merge_recursive(array ...$arrays): array. It accepts multiple array arguments and returns the merged result. String keys are merged, numeric keys are renumbered.

Basic array_merge_recursive Example

This demonstrates simple merging of two arrays with string keys.

basic_merge.php
<?php

$array1 = ['color' => 'red', 'size' => 'small'];
$array2 = ['color' => 'blue', 'shape' => 'round'];

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

print_r($result);

Output shows the 'color' key now contains both values in an array. Other keys are merged normally. This preserves all values from both arrays.

Matching Numeric Keys

When arrays have matching numeric keys, values are appended with new indices.

numeric_keys.php
<?php

$array1 = [0 => 'apple', 1 => 'banana'];
$array2 = [0 => 'orange', 1 => 'pear'];

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

print_r($result);

Numeric keys are renumbered sequentially. The output contains all four fruits with indices 0 through 3. This differs from string key behavior.

Multidimensional Arrays

The function recursively merges nested arrays, combining values at each level.

multidimensional.php
<?php

$array1 = ['fruits' => ['apple', 'banana'], 'vegetables' => ['carrot']];
$array2 = ['fruits' => ['orange'], 'vegetables' => ['celery']];

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

print_r($result);

Both 'fruits' and 'vegetables' subarrays are merged. The result contains all fruits and vegetables from both arrays in their respective categories.

Mixed Key Types

Arrays with both string and numeric keys demonstrate different merging behaviors.

mixed_keys.php
<?php

$array1 = ['name' => 'John', 0 => 'admin'];
$array2 = ['name' => 'Doe', 0 => 'user'];

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

print_r($result);

The 'name' values are combined in an array, while numeric indices are renumbered. This shows how different key types are handled separately.

Deep Recursive Merge

The function handles multiple levels of nesting, merging at each depth.

deep_merge.php
<?php

$array1 = ['user' => ['name' => 'Alice', 'prefs' => ['theme' => 'dark']]];
$array2 = ['user' => ['email' => 'alice@example.com', 'prefs' => ['font' => 'arial']]];

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

print_r($result);

The user data is merged at all levels. Preferences combine both theme and font settings while preserving the nested structure. This demonstrates deep merging.

Best Practices

Source

PHP array_merge_recursive Documentation

This tutorial covered the PHP array_merge_recursive function with practical examples showing its recursive merging behavior with various array types.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PHP Array Functions.