Change language

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

array_merge() function:array_merge() concatenates one or more arrays provided as input and provides a new array as output. This merge process adds array values ​​to the end of the previous array to generate the resulting array.Syntax:
array array_merge ($arr1, $arr2, $arr3 ...) 
Parameters:The array_merge() function takes one or more input arrays and concatenates them into one resulting array.Note.In the array_merge() function, if the input arrays have the same string keys (in the case of categorical arrays), the later key value in the resulting array will overwrite the previous one. But if the array contains numeric keys (in the case of numeric arrays), then the values ​​will not be replaced, they will simply be added to the resulting array. Also, in the case of a numeric array, the value of the keys will be renumbered starting from zero in the resulting array.The array concatenation operator (+).Another way to combine two arrays is the array concatenation operator (+) ... It is a binary operator, which means that it concatenates two arrays at the same time. The union operator appends the right array to the end of the left array.Syntax:
$arr3 = $arr1 + $arr2 
Parameters:the union (+) operator works simultaneously with two arrays and creates the resulting array.Note.In the case of the array union operator (+), keys that match in both arrays, in the resulting array the values ​​from the left array corresponding to the keys will be taken. Also in the case of a numeric array, the index of the right array that is the same as the left array will be ignored in the resulting array.Program:PHP code explaining the difference between array_merge() and array concatenation (+).  
// Combine two arrays where one key (’ one ’) is the 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 );  
// Combine both arrays using the array_merge() function 
// Here in $arr3 the value corresponding
// key ’ one ’ will be from $arr2 and
// numeric keys will be renumbered $arr3 = array_merge ( $arr1 , $arr2 ); echo " Result of array_merge() funcion " ; print_r ( $arr3 );  
// Combine both arrays using the union operator (+)
// Here in $arr4 the value corresponding to the key
// ’ one ’ will be $arr1 and number keys
// which are repeated in $arr2 will be ignored $arr4 = $arr1 + $arr2 ; echo " Result of array union (+) operator " ; print_r ( $arr4 );  
?>
Exit:
Result of array_merge() funcion 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) 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method