PHP array_merge_recursive() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_merge_recursive() merges arrays and, when the same string key appears twice, combines the values into an array instead of replacing them.

It also goes into nested arrays and merges them the same way. Numeric keys are appended and renumbered.

Syntax

Call array_merge_recursive() with the values you need. Start with this line.

PHP
$all = array_merge_recursive($first, $second);
CallWhat it does
array_merge_recursive(["a" => "x"], ["a" => "y"])Returns ["a" => ["x", "y"]].
array_merge_recursive(["a" => 1], ["b" => 2])Returns ["a" => 1, "b" => 2].
array_merge_recursive($defaults, $custom)Merges two nested arrays, collecting values with the same key.

A key that exists in only one array stays a normal value. Only duplicate string keys turn into arrays.

Library declarations
PHP
array_merge_recursive(array ...$arrays): array

Parameters

ParameterDescription
arraysOptional. One or more arrays to merge. With no arguments the result is an empty array.

Return Value

Return ValueDescription
arrayA new array where duplicate string keys hold arrays of all the collected values.

PHP Examples

Example 1 — Collect Values With the Same Key

When two arrays share a key, the values are gathered into a list.

PHP
<?php
$a = ["a" => "x"];
$b = ["a" => "y"];

$result = array_merge_recursive($a, $b);

echo json_encode($result), PHP_EOL;

How It Works

  1. Create two arrays. Both use the key a.
  2. Call array_merge_recursive(). It sees the key twice.
  3. Combine the values. They are placed in a list: x then y.
  4. Print as JSON. The key a now holds an array.

Example 2 — Nested Arrays and Scalar Values

Matching keys are merged at every level, including plain numbers.

PHP
<?php
$a = ["fruits" => ["apple"], "count" => 1];
$b = ["fruits" => ["banana"], "count" => 2];

echo json_encode(array_merge_recursive($a, $b)), PHP_EOL;

How It Works

  1. Use nested arrays. fruits already holds a list.
  2. Merge the lists. apple and banana end up in the same list.
  3. Merge plain numbers. count was 1 and 2, so it becomes [1, 2].
  4. Watch out. Numbers turning into arrays surprises many beginners.

Example 3 — array_merge() vs array_merge_recursive()

Compare the two functions on the same data.

PHP
<?php
$a = ["a" => "x"];
$b = ["a" => "y"];

echo json_encode(array_merge($a, $b)), PHP_EOL;
echo json_encode(array_merge_recursive($a, $b)), PHP_EOL;

How It Works

  1. Use the same arrays. Both have the key a.
  2. Run array_merge(). The later value y replaces the earlier one.
  3. Run array_merge_recursive(). Both values are kept in a list.
  4. Choose carefully. Replace for settings, collect for gathering values.

Frequently Asked Questions

It merges arrays and collects values with the same string key into an array, going into nested arrays as well.
array_merge() replaces earlier values with later ones for the same string key. array_merge_recursive() keeps both values in an array.
When a key appears in more than one array, its values are collected into a list, even if they are plain numbers.
Use array_replace_recursive(), which replaces values at the same key instead of collecting them.
Numeric keys are appended and renumbered, just like array_merge().

Next: array_multisort()

array_multisort() sorts one array, or sorts several arrays together so that matching items.

array_multisort() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful