This tutorial explains how to merge arrays into a single array in PHP. There is a predefined function array_merge(), which is used to merge arrays into one array.
Here is the Syntax of array_merge() Function:
array_merge(array_variable1,array_variable2,array_variable3,...).
Here array_variable1 is the first array varable name,array_variable2 is the second array variable name, array_variable3 is the third array variable name.
Example to Merge Arrays into a Single Array in PHP
The below example demonstrates how to merge two arrays into one array in PHP.
<html> <body> <?php $a=array("name"=>"john","address"=>"kolkata","salary"=>"90000"); $b=array("name1"=>"sebastin","address1"=>"kolkata","salary1"=>"900000"); $c=array(); $c=array_merge($a,$b); print_r($c); ?> </body> </html>
output:
Array ( [name] => john [address] => kolkata [salary] => 90000 [name1] => sebastin [address1] => kolkata [salary1] => 900000 )
Here the first array $a is containing three elements and second array $b is containing three elements. There is a third array $c.
The function array_merge(), which is helping us to append the array elements of the second array to end the first array and then the merged array is assigning to $c. Finally, print_r() is printing the merged array.