Saturday, November 16, 2024
Google search engine
HomeLanguagesWhat is the difference between array_merge and array + array in PHP?

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

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+

RELATED ARTICLES

Most Popular

Recent Comments