Open In App

What is the differences between array_merge() and array_merge_recursive() functions in PHP ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see the array_merge() and array_merge_recursive() functions, along with understanding their basic implementation, & the differences between them.

Both the array_merge() Function and array_merge_recursive() function can be used to combine multiple arrays into a single array.

PHP array_merge() Function

The array_merge function in PHP is a type of function that is used to merge or combine one or many arrays into one single array. This function is used when there are two or more arrays with each array having a different key and we want to display them as one single array. It means that if there are two arrays array A and array B and none of the elements of these two arrays have the same key then using this array_merge function we can combine both arrays and it will be displayed as AB. You can also assign one array to this function.

Example: In this example, we have declared two different arrays with different keys and we have combined them using the array_merge() Function.

PHP




<?php
$a1=array("Mumbai","Nashik");
$a2=array("Nagpur","Pune");
print_r(array_merge($a1,$a2));
?>


Output

Array
(
    [0] => Mumbai
    [1] => Nashik
    [2] => Nagpur
    [3] => Pune
)

PHP array_merge_recursive() Function

The array_merge_recursive() Function in PHP is a type of function that is used to merge or combine one or many arrays into one single array. This function is used when there are two or more arrays with atleast two or more array elements having the same key and we want to display them as one single array. It means that if there are two arrays as array A and array B and at least two elements of these two arrays have the same key then using this array_merge-recursive() function, we can combine both the arrays and it will be displayed as AB. If you assign only one array to this function then it will act the same as that of array_merge().

Example 1: In this example, we have declared two arrays in with two of the elements has the same keys and using the array_merge_recursive() we have combined them successfully.

PHP




<?php
$a1=array("a"=>"Mumbai","b"=>"Nashik");
$a2=array("c"=>"Nagpur","b"=>"Pune");
print_r(array_merge_recursive($a1,$a2));
?>


Output

Array
(
    [a] => Mumbai
    [b] => Array
        (
            [0] => Nashik
            [1] => Pune
        )

     => Nagpur
)

Example 2: The following code shows another example of the array_merge_recursive() method with 4 arrays as parameters.

PHP




<?php
   
// PHP program to demonstrate
// array_merge_recursive()
// function with same keys
$a1 = ["CS" => "Algorithms", "Web Technologies" => "HTML"];
$a2 = ["Maths" => "Probability", "Science" => "Chemistry"];
$a3 = ["Web Technologies" => "PHP", "CS" => "Data Structures"];
$a4 = ["Humanity" => "History", "Science" => "Biology"];
 
print_r(array_merge_recursive($a1, $a2, $a3, $a4));
 
?>


Output:

Array
(
    [CS] => Array
        (
            [0] => Algorithms
            [1] => Data Structures
        )

    [Web Technologies] => Array
        (
            [0] => HTML
            [1] => PHP
        )

    [Maths] => Probability
    [Science] => Array
        (
            [0] => Chemistry
            [1] => Biology
        )

    [Humanity] => History
)

Difference between array_merge() and array_merge_recursive() Functions

array_merge() Functions array_merge_recursive() Functions
This function is used to combine two or more arrays into one single array This function is used to combine multiple arrays such that value of one array is appended to end of last array
This function is used when elements of array has different keys This function is used when elements of array has same keys
Syntax: array_merge($array1, $array2, $array3…..); Syntax: array_merge_recursive($array1, $array2, $array3…..);


Last Updated : 11 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads