PHP array_merge function

Last Updated: February 14, 2022

array_merge – merge one or more arrays together. Values are appended to the end of the one array.

Syntax

 array_merge_recursive(array ...$arrays): array

Supports (PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

Parameters

Parameter Description
arrays Required. list of arrays for merging

Return Value

Returns an array

Example

Merging two arrays. If there are equal keys in two arrays the later value will override the previous value.

<?php

$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge($ar1, $ar2);
print_r($result);

?>

Output

Array ( 
[color] => Array ( 
                   [favorite] => green 
                   [0] => blue 
                 ) 
[0] => 5 
[1] => 10 
) 
Test