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 Callable & Callback: Pass a Function to Another

The callback in PHP lets you pass functions as arguments and reuse code. It helps you to control how tasks…

PHP break Statement: How to Exit a Loop

The PHP break statement helps in controlling the flow of loop work. It gives you a way to stop a…

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 Escape Characters: How to Escape Special Characters

Escape characters appeared in PHP because some symbols in strings serve special purposes. For example, a quote can end a…

PHP Resource Type | How the get_resource_type() Works

In this tutorial, I will explain what does mean the PHP resource and we are going to cover all PHP…

PHP filter_input_array: How to Clean and Validate Input

The filter_input_array() filters multiple inputs in PHP at once. It helps you to clean and validate data. Understand the filter_input_array…

PHP OOP Programming: A Complete Tutorial

A complex web application needs proper organization and maintenance to keep your code structured. This is where PHP Object-Oriented Programming…

PHP $GLOBALS: Access Global Variables with Examples

When you start working with PHP, you’ll soon need access to variables from multiple places in your code. For this,…

PHP $_FILES: How to Upload Files in PHP

The PHP superglobal $_FILES is a very important utility and great for dealing with file uploads on the web. When a user…

How to Remove the Last Character from a String in PHP

You may need to cut the last character from a string if it shows by mistake in PHP. That may…

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.