array_merge() function in PHP

PHPProgrammingServer Side Programming

The array_merge() function merges one or more arrays into one array. It returns an array in which the elements of all arrays passed in parameters are merged.

Note − In case of same keys of two or more array elements, the last one overrides the other.

Syntax

array_merge(arr1, arr2, arr3, …)

Parameters

  • arr1 − Initial array to merge

  • arr2 − Another array

  • arr3 − Another array

Return

The array_merge() function returns an array in which the elements of all arrays passed in parameters are merged.

The following is an example that merges two array with a key repeated in the second array. In this case the last one overrides the other.

Example

Live Demo

<?php
   $arr1 = array("p"=>"red","q"=>"green");
   $arr2 = array("p"=>"blue","r"=>"yellow");
   print_r(array_merge($arr1,$arr2));
?>

Output

Array
(
   [p] => blue
   [q] => green
   [r] => yellow
)
raja
Published on 05-Oct-2018 09:55:45
Advertisements