The Wayback Machine - https://web.archive.org/web/20240824092654/https://www.geeksforgeeks.org/what-is-the-difference-between-array_merge-and-array-array-in-php/
Open In App

What is the difference between array_merge and array + array in PHP?

Last Updated : 10 Apr, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In PHP arrays can be joined using array union (+) operator or by using array_merge() function. There is a subtle differences in these two methods. The array_merge() function is an inbuilt function for joining two array regardless of their type (numerical, categorical etc.)
array_merge() Function: The array_merge() merges one or more arrays provided as input and provide a new array as output. In this merging process the values of an array is appended to the end of the previous array to generate the resulting array.
Syntax: 
 

array array_merge( $arr1, $arr2, $arr3... )

Parameters: The array_merge() function accepts one or more input array and merges them into single resulting array.
Note: In the array_merge() function if the input arrays have same string keys (in case of categorical arrays) then in the resulting array the later value of the key will overwrite the previous one. But if the array contain numeric keys (in case of numeric arrays) then the values will not be replaced they will simply get appended in the resulting array. Also in case of numeric array the value of the keys will be renumbered starting from zero in the resulting array.
Array union (+) operator: Another way to merge two array is the array union(+) operator. It is a binary operator that means it merges two arrays at a time. The union operator appends the right hand array at the end of the left hand array.
Syntax: 
 

$arr3 = $arr1 + $arr2

Parameters: The union(+) operator works on two arrays at a time and produce the resulting array.
Note: In case of array union(+) operator the keys which are same in both the arrays the values from the left hand array corresponding to the keys will be taken in the resulting array. Also in case of numeric array the index of the right hand array which is same with left hand array will be ignored in resultant array.
Program: PHP code to explain difference between array_merge() and array union(+). 
 

php




<?php
 
// Merge two arrays where one key ('one') is same
     
$arr1 = array( 'zero' => 0,
               'one' => 1,
               'two' => 2, 10, 11, 12, 13
        );
         
$arr2 = array( 'one' => 11,
               'three' => 3,
               'four' => 4, 12, 13, 14, 15
        );
     
// Merging both array using array_merge() function
 
// Here in $arr3 the value corresponding to
// the key 'one' will be from $arr2 and
// numeric keys will be renumbered
     
$arr3 = array_merge($arr1, $arr2);
     
echo "Result of array_merge() function\n";
 
print_r($arr3);
     
// Merging both array using array union(+) operator
// Here in $arr4 the value corresponding to the key
// 'one' will be from $arr1 and numeric keys
// which are repeated in $arr2 will be ignored
     
$arr4 = $arr1 + $arr2;
     
echo "\nResult of array union(+) operator\n";
 
print_r($arr4);
 
?>


Output

Result of array_merge() function
Array
(
    [zero] => 0
    [one] => 11
    [two] => 2
    [0] => 10
    [1] => 11
    [2] => 12
    [3] => 13
    [three] => 3
    [four] => 4
    [4] => 12
    [5] => 13
    [6] => 14
    [7] => 15
)

Result of array union(+) operator
Array
(
    [zero] => 0
    [one] => 1
    [two] => 2
    [0] => 10
    [1] => 11
    [2] => 12
    [3] => 13
    [three] => 3
    [four] => 4
)

Let us see the Differences in a Tabular Form -:

  array_merge  array + array
1. The array_merge() function merges one or more arrays into a single array. array + array is also a method to add two arrays and convert them into a single array
2.

Its syntax is -:

array_merge(array1, array2, array3, …)

Here + is the union operator that helps to merge the array
3. It takes arrays into its parameters

Its syntax is -: 

$arr3 = $arr1 + $arr2

4. Its return value is of array type It has a return type as an array because it returns a single array
5. It is Supported in PHP versions 4+ It is Supported in PHP versions 4+


Similar Reads

Difference between array_merge() and array_combine() functions in PHP
array_merge() Function: The array_merge() function is used to merge two or more arrays into a single array. This function is used to merge the elements or values of two or more arrays together into a single array. The merging occurs in such a manner that the values of one array are appended at the end of the previous array. The function takes the l
3 min read
What is the differences between array_merge() and array_merge_recursive() functions in PHP ?
In this article, we will see the array_merge() and array_merge_recursive() functions, along with understanding their basic implementation, &amp; 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. Table of Content PHP array_merge() FunctionPHP
3 min read
How to use array_merge() and array_combine() in PHP ?
In this article, we will discuss about how to use array_merge() and array_combine() functions in PHP. Both functions are array based functions used to combine two or more arrays using PHP. We will see each function with syntax and implementation array_merge() FunctionThis function merges the two or more arrays such that all the arrays have keys and
3 min read
PHP | Merging two or more arrays using array_merge()
The array_merge() is a builtin function in PHP and is used to merge two or more arrays into a single array. This function is used to merge the elements or values of two or more arrays together into a single array. The merging occurs in such a manner that the values of one array are appended at the end of the previous array. The function takes the l
3 min read
Difference between array() and [] in PHP
An array can be created using the array() language construct. It takes any number of comma-separated key =&gt; value pairs as arguments. Syntax: array( key =&gt; value, key2 =&gt; value2, key3 =&gt; value3, ... ) The comma after the last array element is not required can be omitted but this can help to understand other developers that the array is
1 min read
Difference and Similarities between PHP and C
PHP is a server-side scripting language designed specifically for web development. It can be easily embedded in HTML files and HTML codes can also be written in a PHP file. The thing that differentiates PHP from a client-side language like HTML is, PHP codes are executed on the server whereas HTML codes are directly rendered on the browser. C is a
3 min read
Difference between try-catch and if-else statements in PHP
The try and catch are used in PHP for handling exceptions like other languages such as C++, Java, etc. An exception is unexpected result or unexpected state of a program that can be handled by the program itself. To handle this kind of unexpected results in PHP, try and catch are used. For more details, visit Exception Handling in PHP. Similarly, P
5 min read
Difference between isset() and array_key_exists() Function in PHP
isset() function The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases. Syntax: bool isset( $var, mixed ) Parameters: This function a
2 min read
Difference between bindParam and bindValue in PHP
PDOStatement::bindParam() Function The PDOStatement::bindParam() function is an inbuilt function in PHP that is used to bind a parameter to the specified variable name. This function bound the variables, pass their value as input, and receives the output value, if any, of their associated parameter marker.Syntax: bool PDOStatement::bindParam ( $par
3 min read
What is the difference between public, private, and protected in PHP?
Public, private and protected are called access modifiers. Just like C++, PHP also have three access modifiers such as public, private and protected. The visibility of a property, a method or a constant can be defined by prefixing the declaration with these keywords. If the class member declared as public then it can be accessed everywhere.If the c
3 min read
Difference between require-dev and require in PHP?
Before going to understand the difference between require and require_dev first understand what is require and require_dev. require: These are must packages for the code to run. It defines the actual dependency as well as package version. require_dev: It defines the packages necessary for developing the project and not needed in production environm
3 min read
What is the difference between HTTP_HOST and SERVER_NAME in PHP?
HTTP_HOST: It is fetched from HTTP request header obtained from the client request Example: Website: https://www.geeksforgeeks.org HTTP_HOST: www.geeksforgeeks.org HTTP_SERVER: It is fetched from the server name based on the host configuration. Example: Website: https://www.geeksforgeeks.org HTTP_SERVER: Display the server name HTTP_HOST SERVER_NAM
2 min read
What is the difference between is_a() function and instanceof in PHP?
is_a() Function The is_a() is a built-in function in PHP which is used to check whether a given object is of a given class or not. It also checks if the given class is one of the parents of the given object or not. Syntax: bool is_a( $object, $class_name, $allow_string ) Parameters: This function accepts three parameters as mentioned above and desc
3 min read
What is the difference between fopen() and fclose() functions in PHP ?
In this article, we are going to discuss the differences between fopen() and fclose() functions. fopen() function: This function helps users to open a file or a web URL. It mainly accepts two parameters - One is $file that specifies the file or an URL to be opened and the other is $mode that specifies in which mode the file needs to be opened. Let'
2 min read
What is the difference between echo, print, and print_r in PHP?
echo: echo is not a function rather it is described as a language construct. It accepts an list of argument (multiple arguments can be passed) and returns no value or returns void. It cannot be used as a variable function in PHP. It is used to display the output of parameters that is passed to it. It display the outputs one or more strings separate
2 min read
What is the difference between a language construct and a “built-in” function in PHP ?
In programming, language constructs and built-in functions are often misinterpreted with one another due to the fact that both have more or less alike behavior. But they differ from each other in the way the PHP interpreter interprets them. Every programming language consists of tokens and structures which the respective language parser can recogni
3 min read
Difference between “!==” and “==!” in PHP
!== Operator: It is called as non-identical operator. It returns true if operands are not equal, or they are not of the same type. Syntax: $x !== $y Where $x and $y are the operands. ==! Operator: It is nothing but it can be further written as ==(!operand) which returns true or false depending on operands. Both the operators returns the boolean val
3 min read
What is the difference between single-quoted and double-quoted strings in PHP?
Single or double quotes in PHP programming are used to define a string. But, there are lots of differences between these two. Single-quoted Strings: It is the easiest way to define a string. You can use it when you want the string to be exactly as it is written. All the escape sequences like \r or \n, will be output as specified instead of having a
3 min read
Difference between break and continue in PHP
The break and continue both are used to skip the iteration of a loop. These keywords are helpful in controlling the flow of the program. Difference between break and continue: The break statement terminates the whole iteration of a loop whereas continue skips the current iteration.The break statement terminates the whole loop early whereas the cont
2 min read
What is the difference between Perl and PHP ?
PHP: PHP (Hypertext Pre-processor) is a general-purpose programming language used to designed a website or web applications. It is the server-side scripting language embedded with HTML to develop Static website, Dynamic website or Web applications. It is used to interact with database and display the data. PHP is essentially utilized for creating w
2 min read
Difference Between JSP and PHP
JSP was an implies of giving a comparable programming fashion to PHP and ASP. It is based on Java Servlets and requires a Servlet holder server like Tomcat to supply the backend preparation required to change over the JSP to a servlet that can yield HTML. In differentiating PHP can run on its possess as a CGI motor, or as a coordinates module in nu
4 min read
What is the difference between fopen modes “r+”, "rw+" and “w+” in PHP?
File handling is a very important part of programming when it comes to making applications. We might require reading input from a file or writing output into a file. File handling in PHP is similar to other programming languages. There are predefined functions that can be used to accomplish the specified task. Check out the Basics of File Handling
2 min read
Difference Between “php artisan dump-autoload” and “composer dump-autoload”
PHP Artisan: Artisan is a command-line UI that can aid you while you build your application. PHP artisan command performs multiple tasks in less time and is more efficient. It can be used to create a model, to create a cache, to create a controller, and can also be used to create Laravel packages and add dummy data too. PHP artisan serve: This comm
4 min read
Difference between file_get_contents and cURL in PHP
file_get_contents() Function: This PHP function is used to retrieve the contents of a file. The contents can be stored as a string variable. Alternatively, it also simulates HTTP transactions, involving requests via GET method and responses using POST method respectively. Primarily, it is best-suited for simple HTTP manipulations and to get single-
2 min read
Difference between self::$bar and static::$bar in PHP
self keyword: It is a PHP keyword that represents the current class and used to access static class variables or static variables because these members belong to a class rather than the object of that class. Example: &lt;?php // Declaring parent class class demo { public static $bar = 10; public static function func() { echo self::$bar . &quot;\n
2 min read
Difference Between Laravel and CodeIgniter Framework in PHP
Laravel Laravel is a PHP based framework. It is developed by Taylor Otwell June 2011 and it is free to open-source PHP web framework as well as supports model-view-controller (MVC) patterns for application development. This framework mostly used for developing a modest and full-featured application for web .laravel is written in PHP, and it is used
2 min read
Difference between PHP and DENO
PHP: PHP stands for Hypertext preprocessor, which is a scripting language. PHP is a general-purpose scripting language. PHP was created by Rasmus Lerdorf in 1994. PHP is basically used on the server-side for the development of web applications. PHP has certain frameworks like laravel, Symfony etc. Please note that PHP is a scripting language and no
2 min read
Difference between Java and PHP
Java is a very famous object-oriented programming language. It was developed by Sun Microsystems. It has a virtual machine platform that allows you to create compiled programs that run on nearly every platform. Java promised, “Write Once, Run Anywhere”. It has a continuous contribution from industry experts, Java developers, and other open-source o
2 min read
Difference between Django and PHP
In the present world, many new frameworks have emerged in web technology. One such framework is Django which is based on Python. PHP has been in use for several years and has been serving as a powerful scripting language especially for backend connectivity. This article compares and contrasts Django and PHP. Django Django is a high-level python bas
3 min read
Difference Between Golang and PHP
Golang is a statically typed, compiled programming language invented at Google headquarter by Mr. Robert Griesemer, Mr Rob Pike, and Mr. Ken Thompson. Its development began in 2007 and it was made available to the public in the year 2009. It's open-source and freely available for public use. It is mostly congruent to C and C++ programming language
2 min read