Open In App

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 list of arrays separated by commas as a parameter that are needed to be merged and returns a new array with merged values of arrays passed in parameter.

Syntax:



array array_merge($array1, $array2, ......, $arrayn)

Parameters: The array_merge() function takes a list of arrays separated by commas as a parameter that are needed to be merged as shown in the syntax. There are n arrays (($array1, $array2, ……, $arrayn) separated by (‘,’) in the syntax. We can pass any number of arrays in parameter.

Return Value: It returns a new array in which the elements of all arrays passed in parameters are merged such that the values of one array are appended at the end of the previous array.



Below programs illustrates the working of array_merge() function in PHP:


Article Tags :