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
Example:
<?php
$array1=array("Subject" => "Physics","Chemistry", "Biology");
$array2=array("Class-XI", "Class-XII", "Section"=>"A");
$result=array_merge($array1, $array2);
print_r($result);
?>
Output:
Array ( [Subject] => Physics [0] => Chemistry [1] => Biology [2] => Class-XI [3] => Class-XII [Section] => A )
Pictorial Presentation:

View the example in the browser
Practice here online :
See also
Previous: array_merge_ recursive
Next: array_multisort
PHP: Tips of the Day
Filters the collection using the given callback
Example:
<?php function tips_reject($items, $func) { return array_values(array_diff($items, array_filter($items, $func))); } print_r(tips_reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) { return strlen($item) > 4; })); ?>
Output:
Array ( [0] => Pear [1] => Kiwi )
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises