PHP array_merge: Combine Multiple Arrays into One

PHP array_map Function

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?

No. It does not change any of the arrays you pass to it. It makes a new one.

What happens if I pass non-array values?

PHP throws a warning. You must pass arrays.

Is array_merge the same as + operator for arrays?

No. The + operator keeps keys from the first array. It ignores duplicates. array_merge replaces them.

Can I merge arrays inside a loop?

Yes. But make sure the result goes into a new array each time.

How does PHP treat string keys vs. numeric keys?

String keys keep their name. Numeric ones reset.

Similar Reads

PHP array_intersect_uassoc: How it Works with Examples

PHP array_intersect_uassoc checks two or more arrays and returns matches. It compares both the values and the keys with a…

PHP array_filter: How to Filter Array Values with Examples

You can use array_filter in PHP to remove unwanted data from arrays. It works with a custom callback or default…

PHP Static Property: How It Works & Examples

PHP static property helps you manage shared data across instances, but can lead to hidden state changes. In this article,…

PHP array_map Function: How to Transform Arrays with Examples

PHP added the array_map() function to help you apply one function to each item in an array. Understand the array_map…

PHP array_diff_assoc: How to Compare Arrays with Keys

Array values may look the same, but keys can differ. The array_diff_assoc in PHP finds differences by both values and…

PHP Object | How to Create an Instance of a Class

The PHP object is an instance from the PHP class or the main data structure that is already been created…

PHP $_ENV: Manage Environment Variables

In this tutorial, we will explore a form of superglobal variable in PHP: $_ENV, which symbolizes how PHP reaches for…

PHP array_key_exists: How it Works with Examples

The PHP array_key_exists function checks a given key in an array and tells if that key exists or not. Understand…

Understanding the PHP Enumerable

Actually, the PHP enumerable is a new feature of 8.1 which allows you to define a new type. This feature…

Parameters and Arguments in PHP: What’s the Difference?

If you're coding in PHP you've most probably come across the terms 'parameters' and 'arguments' in functions. Well, they are…

Previous Article

Ajax and PHP: How Ajax Works with PHP with Examples

Next Article

PHP str_replace Function: How it Works with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.