Skip to main content

PHP array_merge_recursive() Function — Explained

PHP array_merge_recursive() — Preserve Numeric Keys

📖 PHP array_merge_recursive() Function — Explained

The array_merge_recursive() function in PHP is used to merge two or more arrays recursively. It helps you combine values under the same keys without overwriting any data.

Unlike array_merge(), which replaces duplicate keys, this function keeps both values by converting them into arrays.

🔑 Keyword Focus: php array_merge_recursive preserve numeric keys

If you're trying to preserve numeric keys while merging arrays in PHP, it's important to know that array_merge_recursive() will reindex numeric keys by default. Let’s see what that means.

📌 Syntax

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

Parameters:

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

Returns:

  • New merged array.
  • String keys: merged into arrays.
  • Numeric keys: reindexed and appended.

📌 Example 1: Simple Merge Without Conflicting Keys

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

Output:

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

Explanation: "color" exists in both arrays, so its values are combined. Numeric keys are reindexed sequentially.

📌 Example 2: Nested Array Merge

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

Output:

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

Explanation: Since the "details" key contains arrays in both, they are merged recursively into one array.

📌 Example 3: Conflicting Keys (String and Numeric)

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

Output:

Array ( [fruit] => Array ( [0] => apple [1] => banana ) [0] => orange [1] => grape )
Note: Numeric keys like 0 are not preserved. They are renumbered.

📦 Use Cases

  • Merging form data from different sources
  • Combining multiple config files
  • Joining nested arrays from API responses

⚠️ Important Notes

  • Recursively merges arrays at all levels
  • Can cause deep nesting if keys repeat
  • Numeric keys are not preserved — use $array1 + $array2 if needed

📌 Conclusion

array_merge_recursive() is great for merging arrays deeply without losing values. But if you're concerned about numeric key preservation, this function may not be ideal.

For preserving numeric keys, use the + operator or build a custom function that suits your logic.

📘 Official PHP Docs

Visit PHP Manual

💡 Keyword Recap:

  • php array_merge_recursive preserve numeric keys
  • php merge arrays without overwriting
  • php array merge nested

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...