What is the differences between array_merge() and array_merge_recursive() functions in PHP ?
Last Updated :
11 Dec, 2023
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));
?>
OutputArray
(
[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));
?>
OutputArray
(
[a] => Mumbai
[b] => Array
(
[0] => Nashik
[1] => Pune
)
[c][/c] => 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
|
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.....); |
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read