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
The PHP object is an instance from the PHP class or the main data structure that is already been created…
PHP developers may be need to determine the number of elements in an array. The count() function in PHP makes…
Before the spread operator, PHP developers had to pass array items to a function using extra steps. They often had…
Sometimes, you may need to list collections with MongoDB in a PHP environment to manage or analyze your database structure.…
When you start working with PHP, you’ll soon need access to variables from multiple places in your code. For this,…
The filter_input_array() filters multiple inputs in PHP at once. It helps you to clean and validate data. Understand the filter_input_array…
The filter_var_array() appeared to make input validation simple and sanitization in PHP. It handles multiple inputs with different filters was…
PHP array_push lets you add one or more values to the end of an array. It appeared to make adding…
PHP introduces the filter_list() function to give developers a way to check all available filters in the filter extension. This…
Keeping track of the last record inserted is often important. Whether you are managing user registrations, processing orders, or handling…