Array merge multiple arrays php
View Discussion Show Improve Article Save Article View Discussion Improve Article Save Article 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:
❮ PHP Array Reference ExampleMerge two arrays into one array: $a1=array("red","green"); Try it Yourself » Definition and UsageThe array_merge() function merges one or more arrays into one array. Tip: You can assign one array to the function, or as many as you like. Note: If two or more array elements have the same key, the last one overrides the others. Note: If you assign only one array to the array_merge() function, and the keys are integers, the function returns a new array with integer keys starting at 0 and increases by 1 for each value (See example below). Tip: The difference between this function and the array_merge_recursive() function is when two or more array elements have the same key. Instead of override the keys, the array_merge_recursive() function makes the value as an array. Syntaxarray_merge(array1, array2, array3, ...) Parameter Values
Technical Details
More ExamplesExampleMerge two associative arrays into one array: $a1=array("a"=>"red","b"=>"green"); Try it Yourself » ExampleUsing only one array parameter with integer keys:
$a=array(3=>"red",4=>"green"); Try it Yourself » ❮ PHP Array Reference Can we merge more than two array in PHP?The array_merge() function merges one or more arrays into one array. Tip: You can assign one array to the function, or as many as you like. Note: If two or more array elements have the same key, the last one overrides the others.
How can I merge two arrays in PHP without duplicates?You can use the PHP array_unique() function and PHP array_merge() function together to merge two arrays into one array without duplicate values in PHP.
What is the difference between array_merge () and Array_merge_recursive () in PHP?The array_merge_recursive() function merges one or more arrays into one array. The difference between this function and the array_merge() function is when two or more array elements have the same key. Instead of override the keys, the array_merge_recursive() function makes the value as an array.
How do you merge arrays?To merge elements from one array to another, we must first iterate(loop) through all the array elements. In the loop, we will retrieve each element from an array and insert(using the array push() method) to another array. Now, we can call the merge() function and pass two arrays as the arguments for merging.
|