Skip to main content

PHP array_merge_recursive() Function — Explained

📖 PHP array_merge_recursive() Function — Explained

The array_merge_recursive() function in PHP is used to merge two or more arrays.
Unlike array_merge(), which replaces the values of duplicate keys, array_merge_recursive() combines values with the same keys into an array.


📌 Syntax:


array_merge_recursive(array $array1, array $array2, ...): array

Parameters:

  • $array1, $array2, ... → Arrays you want to merge.

Returns:

  • A new merged array. If two arrays have the same string keys, their values will be merged into an array instead of being overwritten.


📖 How It Works:

  • If keys are different: Values are simply added to the result.

  • If keys are the same:

    • If the key is a string → The values are merged into an array.

    • If the key is numeric → The values are appended.


📌 Example 1: Simple Merge Without Conflicting Keys


<?php $array1 = array("color" => "red", 2, 4); $array2 = array("a", "b", "color" => "green", "shape" => "circle"); $result = array_merge_recursive($array1, $array2); print_r($result); ?>

✅ Output:

Array ( [color] => Array ( [0] => red [1] => green ) [0] => 2 [1] => 4 [2] => a [3] => b [shape] => circle )

Explanation:

  • The key "color" exists in both arrays, so their values ("red" and "green") are combined into an array.

  • Numeric keys are appended sequentially.


📌 Example 2: Nested Array Merge


<?php $array1 = array("details" => array("name" => "John", "age" => 25)); $array2 = array("details" => array("city" => "Karachi")); $result = array_merge_recursive($array1, $array2); print_r($result); ?>

✅ Output:


Array ( [details] => Array ( [name] => John [age] => 25 [city] => Karachi ) )

Explanation:

  • The "details" key exists in both arrays. Since their values are arrays themselves, array_merge_recursive() merges them.


📌 Example 3: Conflicting Keys (String and Numeric)


<?php $array1 = array("fruit" => "apple", 0 => "orange"); $array2 = array("fruit" => "banana", 0 => "grape"); $result = array_merge_recursive($array1, $array2); print_r($result); ?>

✅ Output:


Array ( [fruit] => Array ( [0] => apple [1] => banana ) [0] => orange [1] => grape )

📌 Use Cases:

  • Combining form inputs from multiple sources.

  • Merging configuration arrays without losing values.

  • Combining nested data structures (e.g., merging API responses).


📌 Important Notes:

  • It recursively merges arrays. If values are arrays themselves, it will continue to merge them at deeper levels.

  • Be cautious: It can result in deeply nested structures if arrays have the same keys at multiple levels.

  • If you don’t want this recursive merging behavior, use array_merge() instead.


📌 Conclusion:

array_merge_recursive() is a powerful function when working with complex or nested arrays, where you want to preserve all values, including those under the same keys, by combining them into arrays instead of overwriting.


📌 Official PHP Docs:

👉 https://www.php.net/manual/en/function.array-merge-recursive.php

Comments

Popular posts from this blog

PHP array_column() Function

  <?php // An array that represents a possible record set returned from a database $a =  array (    array (      'id'  =>  5698 ,      'first_name'  =>  'Mehran' ,      'last_name'  =>  'Yaqoob' ,   ),    array (      'id'  =>  4767 ,      'first_name'  =>  'Muneeb' ,      'last_name'  =>  'Ahmad' ,   ),    array (      'id'  =>  3809 ,      'first_name'  =>  'Uzair' ,      'last_name'  =>  'Rajput' ,   ) ); $last_names = array_column($a,  'last_name' ); print_r($last_names); ?> Output: Array (   [0] => Yaqoob   [1] => Ahmad   [2] => Rajput ) Syntex: array_column( array ,  column_key ,  index_key ...

Mastering PHP's array_merge(): When to Use It (And When Not To)

Pros, Cons, and Best Practices PHP's  array_merge()  function is one of the most commonly used array functions in web development. Whether you're building a simple website or a complex web application, understanding how to effectively merge arrays can save you time and prevent headaches. In this comprehensive guide, we'll explore everything you need to know about  array_merge() , including its advantages, limitations, practical use cases, and alternatives. What is  array_merge()  in PHP? array_merge()  is a built-in PHP function that combines two or more arrays into a single array. The function takes multiple array arguments and returns a new array containing all the elements from the input arrays. Basic Syntax php array_merge ( array ... $arrays ) : array Simple Example php $array1 = [ 'a' , 'b' , 'c' ] ; $array2 = [ 'd' , 'e' , 'f' ] ; $result = array_merge ( $array1 , $array2 ) ; print_r ( $result ) ; /* O...