PHP array_merge() Function

6 Jan 2025 | 1 min read

The array_merge( ) function is a built-in function of PHP. This function is used to merge the elements or values of two or more arrays together into a single array. This function was introduced in PHP 4.0.

Syntax

Parameter

ParameterDescriptionIs compulsory
array1Specifies an array.compulsory
array2Specifies an array.Optional
array3,...Specifies an array.Optional

Return Type

The array_merge( ) function returns the merged array.

Note: If two or more array elements have the same key, the last one overrides the others.

EXAMPLE 1

Output:

 Array ( [a] => cricket [b] => tennis [c] => football ) 

EXAMPLE 2

Output:

 Array ( [0] => java [1] => T [2] => point ) 

EXAMPLE 3

Output:

 Array( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 ) 

EXAMPLE 4

Output:

 Array ( [sachin] => arjun [0] => 15921 [1] => 200 [2] => virat [3] => ganguly [yuvraj] => world cup [4] => 2011 )