Home » Programming » PHP » Php Array_merge

Merging/Combining Arrays in PHP with array_merge [Examples]

This article will explain how to merge arrays using the PHP array_merge function and provide some code examples.

What is an Array?

An array is a type of PHP variable that can hold other values, or references to other variables, in a list at a certain position.

Arrays are really useful – allowing you to store a list of variables or values of any length in a single variable – ready to be iterated over, printed as the rows in a table, or otherwise processed.

Arrays might contain a list of students in a class, which can be managed by adding or removing students, or they might contain a list of enemies to be shown on-screen in a video game – ready to be removed from the array as they are defeated.

Arrays form the backbone of pretty much every application you might write in PHP or any other programming language.

Merging/Combining Arrays

Being able to merge arrays is useful functionality. PHP has a built-in function called array_merge, which does just this.

PHP array_merge Function & Syntax

Here’s the syntax for the array_merge function in PHP:

?array_merge($array1, $array2, ...)

Note that:

  • array_merge will return an array containing the contents of the arrays passed to it merged into a single array
  • $array1$array2, etc. are the arrays you wish to merge
    • One or more arrays should be supplied.
    • If only one array is supplied, it will be returned as-is
    • If no arrays are supplied, an empty array will be returned
    • There is no limit to the number of arrays that can be passed
  • The arrays will be merged in the order they are supplied to array_merge
  • Arrays will be re-indexed when merged! If your array uses numeric keys, they will be changed as each item in the result array will now have a new position
  • If associative arrays are being merged, the value in the last array passed as a parameter will overwrite the value in any previous arrays being merged if they use the same key

Examples

Merging arrays with array_merge:

Below, two arrays are defined and then merged:

$array1 = ['dog', 'cat', 'chicken'];
$array2 = ['pig', 'cow'];

$merged = array_merge($array1, $array2);

print_r($merged);

This will result in the following output:

Array
(
    [0] => dog
    [1] => cat
    [2] => chicken
    [3] => pig
    [4] => cow
)

As you can see, the arrays have been merged in the order they were supplied.

Note that the print_r command is used to print the array – this command prints the array recursively, including the value of the array and the values stored in it, along with their index.

Merging Associative Arrays

The below example will merge three arrays, with some values keyed with strings instead of numbers (associative arrays):

$array1 = [
    'fox', 
    'crow',
    'building' => 'barn'
];
$array2 = [
    'sky' => 'blue',
    'carrot',
    'wheat'
];

$merged = array_merge($array1, $array2);

print_r($merged);

This will result in the merged array being output:

Array
(
    [0] => fox
    [1] => crow
    [building] => barn
    [sky] => blue
    [2] => carrot
    [3] => wheat
)

Numerical keys have been re-indexed, and string (associative) indexes (buildingsky) remain unmodified.

If, however, there were duplicate associative values in the arrays, the last value will overwrite any previous values:

$array1 = [
    'fox', 
    'crow',
    'building' => 'barn'
];
$array2 = [
    'building' => 'house',
    'carrot',
    'wheat'
];

$merged = array_merge($array1, $array2);

print_r($merged);

Which will output:

Array
(
    [0] => fox
    [1] => crow
    [building] => house
    [2] => carrot
    [3] => wheat
)

…Despite there being two values for the key building in the arrays before merging, only the last value is kept as you cannot have conflicting identical array keys.

The above syntax and methods will also work with multidimensional arrays.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment