PHP array_merge_recursive() – Recursively Merges One or More Arrays

The article explains how PHP's array_merge_recursive() function combines multiple arrays by recursively merging values with identical keys, describes its parameters and return value, and provides a complete code example with expected output.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP array_merge_recursive() – Recursively Merges One or More Arrays

The PHP function array_merge_recursive() merges one or more arrays recursively, appending values from later arrays to earlier ones and combining values with identical string keys into sub‑arrays.

Parameters array1 – the initial array to merge. ... – additional arrays to be merged recursively.

Return value

An array containing the merged values.

Example

<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
?>

The output shows that the color key now holds an array with both favorite values (“red” and “green”) and the additional blue entry, while numeric values are appended.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendprogrammingPHPRecursionphp-arrayarray_merge_recursive
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.