Friday, October 24, 2025
HomeLanguagesWhat is the differences between array_merge() and array_merge_recursive() functions in PHP ?

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

PHP array_merge(): 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 different 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 none of the elements of this two arrays have the same key then using this array_merge function we can combine this both the arrays and it will be displayed as AB. You can also assign one array to this function.

Example:  So in the following code we have declared two different arrays with different keys and we have combined them using the array_merge()

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(): 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 have 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 atleast two elements of this two arrays have the same key then using this array_merge-recursive() function we can combine this both the arrays and it will be displayed as AB. If you assign only one array to this function then it will act same as that of array_merge().

Example: So in the following code 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
)

Difference between array_merge() and array_merge_recursive():

array_merge() array_merge_recursive()
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…..);
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS