You have two or more arrays. You want one. That is the problem array_merge solves in PHP. This function lets you combine arrays into one result. You use it when you need a clean and full merge.
Understand the array_merge Function in PHP
array_merge puts arrays together. It adds values from one into another. If both arrays use the same keys, it picks values from the last one. You get a single array as output.
Here is the syntax:
array_merge($array1,$array2, ...$arrays)- You must pass at least one array.
- You can pass more arrays if you want.
- The function adds values in order.
It returns one array. That array holds all values from the input arrays. If it sees repeated string keys, it keeps the one from the last array.
For example:
$first = ['PHP', 'JS'];
$second = ['PYTHON', 'C#'];
$langs = array_merge($first, $second);
print_r($langs);Output:
Array
(
[0] => PHP
[1] => JS
[2] => PYTHON
[3] => C#
)
Each array adds its values to the final result and indexes reset from zero.
If both arrays use numbers as keys, PHP throws them out and starts from zero again. The order stays, but the indexes change.
$a = [3 => 'x', 4 => 'y'];
$b = [7 => 'z'];
$result = array_merge($a, $b);
print_r($result);Output:
Array
(
[0] => x
[1] => y
[2] => z
)
You see the same values. But the keys now go from 0 to 2.
If arrays use named keys, PHP keeps the last value it finds.
$a = ['type' => 'fruit', 'name' => 'apple'];
$b = ['name' => 'banana', 'color' => 'yellow'];
$result = array_merge($a, $b);
print_r($result);Output:
Array
(
[type] => fruit
[name] => banana
[color] => yellow
)
The key name appears in both arrays. The second one wins. Others stay untouched.
PHP array_merge Examples
Mixed arrays (indexed + associative): You can pass both types in one call. PHP adds all values and applies the same rules.
$first = ['a', 'b', 'c'];
$second = ['type' => 'letter', 1 => 'd'];
$result = array_merge($first, $second);
print_r($result);Output:
Array
(
[0] => a
[1] => d
[2] => c
[type] => letter
)
Key 1 appears in both. The second one replaces the first. Named keys go in as-is.
Associative arrays: Only keys matter here. If both arrays use the same key, the second one overwrites the first.
$settingsA = ['mode' => 'dark', 'font' => 'Arial'];
$settingsB = ['font' => 'Verdana', 'size' => 12];
$finalSettings = array_merge($settingsA, $settingsB);
print_r($finalSettings);You get the new font and keep the old mode.
Merging two indexed arrays: PHP drops the old keys and starts fresh.
$week1 = ['Mon', 'Tue'];
$week2 = ['Wed', 'Thu'];
$week = array_merge($week1, $week2);
print_r($week);Output:
Array
(
[0] => Mon
[1] => Tue
[2] => Wed
[3] => Thu
)
It keeps the order. Keys reset to match that order.
Merging more than two arrays: You can pass three, four, or more arrays. PHP keeps going until it merges all.
$a = ['red', 'blue'];
$b = ['green'];
$c = ['yellow', 'black'];
$colors = array_merge($a, $b, $c);
print_r($colors);Output:
Array
(
[0] => red
[1] => blue
[2] => green
[3] => yellow
[4] => black
)
You can mix and match arrays. PHP keeps the order you pass them in.
Wrapping Up
In this article, you learn how array_merge works and how it treats different types of arrays. You see what happens with indexed arrays, with named keys, and when both types appear in one merge.
Here is a quick recap:
- It combines arrays in order.
- It resets numeric keys.
- It keeps the last value for each repeated named key.
- You can pass more than two arrays at once.
Thank you for reading. Here you will find more PHP tutorials.
FAQs
Does array_merge change the original arrays?
What happens if I pass non-array values?
Is array_merge the same as + operator for arrays?
Can I merge arrays inside a loop?
How does PHP treat string keys vs. numeric keys?
Similar Reads
PHP array_intersect_uassoc checks two or more arrays and returns matches. It compares both the values and the keys with a…
You can use array_filter in PHP to remove unwanted data from arrays. It works with a custom callback or default…
PHP static property helps you manage shared data across instances, but can lead to hidden state changes. In this article,…
PHP added the array_map() function to help you apply one function to each item in an array. Understand the array_map…
Array values may look the same, but keys can differ. The array_diff_assoc in PHP finds differences by both values and…
The PHP object is an instance from the PHP class or the main data structure that is already been created…
In this tutorial, we will explore a form of superglobal variable in PHP: $_ENV, which symbolizes how PHP reaches for…
The PHP array_key_exists function checks a given key in an array and tells if that key exists or not. Understand…
Actually, the PHP enumerable is a new feature of 8.1 which allows you to define a new type. This feature…
If you're coding in PHP you've most probably come across the terms 'parameters' and 'arguments' in functions. Well, they are…