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

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically