Backend Development 4 min read

Using PHP array_merge() to Combine Multiple Arrays

This article explains the PHP array_merge() function, shows its syntax, and provides three practical examples—including merging two indexed arrays, merging several arrays, and merging associative arrays—while illustrating the resulting output and key‑overriding behavior.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP array_merge() to Combine Multiple Arrays

PHP provides many powerful functions for handling arrays, and one of the most useful is array_merge() , which merges one or more arrays into a new array and returns the result.

The syntax of array_merge() is straightforward:

array_merge ( array $array1 [, array $... ] ) : array

The function accepts multiple arrays as arguments and returns a single combined array.

Below are several example codes demonstrating how to use array_merge() :

Example 1: Merging Two Arrays

$array1 = array('apple', 'banana', 'orange');
$array2 = array('kiwi', 'melon', 'grape');
$result = array_merge($array1, $array2);
print_r($result);

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => kiwi
    [4] => melon
    [5] => grape
)

In this example, the two arrays $array1 and $array2 are combined into a new array $result , which contains all elements from both original arrays.

Example 2: Merging 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:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => kiwi
    [4] => melon
    [5] => grape
    [6] => strawberry
    [7] => pineapple
)

This example shows that any number of arrays can be merged; the resulting array contains all elements in the order they were provided.

Example 3: Merging Associative Arrays

$array1 = array('name' => 'John', 'age' => 25);
$array2 = array('name' => 'Jane', 'email' => '[email protected]');
$result = array_merge($array1, $array2);
print_r($result);

Output:

Array
(
    [name] => Jane
    [age] => 25
    [email] => [email protected]
)

When merging associative arrays, if the same key appears in multiple arrays, the value from the later array overwrites earlier ones; thus the key name ends up with the value Jane in the result.

Summary

The array_merge() function is a versatile tool for combining both indexed and associative arrays in PHP. It simplifies data handling by allowing developers to merge any number of arrays into a single, cohesive structure.

PHP Introductory Tutorial – Learn PHP in One Week

Scan the QR code to receive free learning materials

BackendPHPArrayCode Examplephp-functionsarray_merge
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.