PHP array_merge() Function

PHP array_merge() Function is used to merge one or more arrays into one array. PHP array_merge() function is PHP built-in function.

Syntax :

array_merge(array  $array1, array  $array2, array $array3, ...);

Parameter,

$array1 : Required. It specifies array.

$array2 : Required. It specifies array.

$array3, ... : Required. It specifies array.

Return :  It returns merged array by joining arrays together as per as below.

It merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one.

If the parameter arrays have the same string keys, then the later value for that key will overwrite the previous one.

If you assign only one array to the array_merge() function and the keys are integers, then the function returns a new array with integer keys starting at 0 and increases by 1 for each value.

Let's see below example to understand php array_merge() Function in details.

Example :

<?php
$array1 = array("fruit"=>"apple","color"=>"black","animal"=>'cow',5,"3"=>"php");
$array2 = array("color"=>"white","5"=>"tree","game"=>'cricket',10);
$merged_array=array_merge($array1 ,$array2);
echo "<br>Get new array by merging two arrays using array_merge() function<br>";
print_r($merged_array);
?>

Output :

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

61525