PHP array_merge() Function: Syntax, Examples, and Usage Guide
This article explains the PHP array_merge() function, its simple syntax, and demonstrates how to merge indexed, multiple, and associative arrays with clear code examples, showing the resulting output and highlighting key behaviors such as key overriding.
PHP provides many powerful functions for handling arrays, and one very useful function is array_merge() . This function can merge multiple arrays into a new array and return that new array. In this article we will explore the usage of array_merge() and several examples.
The syntax of array_merge() is very simple:
array_merge ( array $array1 [, array $... ] ) : arrayThe array_merge() function accepts multiple arrays as parameters and returns a merged new array.
Below are some example codes using array_merge() :
Example 1: Merge two arrays
$array1 = array('apple', 'banana', 'orange');
$array2 = array('kiwi', 'melon', 'grape');
$result = array_merge($array1, $array2);
print_r($result);Output result:
Array
(
[0] => apple
[1] => banana
[2] => orange
[3] => kiwi
[4] => melon
[5] => grape
)In this example we have two arrays $array1 and $array2 . By calling array_merge() we combine them into a new array $result , which contains all elements from the original arrays.
Example 2: Merge multiple arrays
$array1 = array('apple', 'banana', 'orange');
$array2 = array('kiwi', 'melon', 'grape');
$array3 = array('strawberry', 'pineapple');
$result = array_merge($array1, $array2, $array3);
print_r($result);Output result:
Array
(
[0] => apple
[1] => banana
[2] => orange
[3] => kiwi
[4] => melon
[5] => grape
[6] => strawberry
[7] => pineapple
)This example shows three arrays $array1 , $array2 , and $array3 . Calling array_merge() merges them into a single new array $result , which includes every element from the three source arrays.
Example 3: Merge associative arrays
$array1 = array('name' => 'John', 'age' => 25);
$array2 = array('name' => 'Jane', 'email' => '[email protected]');
$result = array_merge($array1, $array2);
print_r($result);Output result:
Array
(
[name] => Jane
[age] => 25
[email] => [email protected]
)Here we have two associative arrays $array1 and $array2 . Both contain the same key name . When merged with array_merge() , the later array’s value overwrites the earlier one, so the resulting array keeps the last value for duplicate keys.
Summary
The array_merge() function is a very useful tool for combining multiple arrays into a new array. It works with both indexed and associative arrays, offering a concise and efficient way to handle data. In practice you can merge as many arrays as needed, making data manipulation more flexible and convenient.
Java learning material download
C language learning material download
Frontend learning material download
C++ learning material download
PHP learning material download
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.