PHP array merge

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP: array_merge() function

PHP: Merge one or more arrays

The array_merge() function used to merge one or more arrays.
If the input arrays have matching string keys, then the later value will override it's the previous counterpart.
If the input arrays contain numeric keys, the later value will be appended instead of overriding the original value.
If there is only one array, the array is numerically indexed, the keys get reindexed in a continuous way.

Version

(PHP 4 and above)

Syntax

array_merge(array_name1, array_name2, array_name3...) 

Parameters

Name

Description

Required /
Optional

Type

array_name1

The name of the array.

Required

Array

array_name2...

The name of the array.

Optional

Array

Return value

The merged array.

Value Type : Array

Pictorial Presentation :

php array array_merge() function

Example :

  1. $array1=array("Subject" => "Physics","Chemistry", "Biology");  

  2. $array2=array("Class-XI", "Class-XII", "Section"=>"A");  

  3. $result=array_merge($array1, $array2);  

  4. print_r($result);  

  5. ?>  

Output :

Array ( [Subject] => Physics [0] => Chemistry [1] => Biology [2] => Class-XI [3] => Class-XII [Section] => A )