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 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…

How to Update MySQL Data with PHP?

Understanding how to update data in PHP and MySQL is like editing a draft—it is all about tweaking the right…

IF-Else: Executes the IF-Else Statement in PHP

Programming is all about choices. Everything you do in code boils down to answering a question: What should happen if…

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 error_log Function: How It Works with Examples

Sometimes things go wrong when PHP runs the code. So, you need to know the reasons for this error to…

PHP is_file Function: Check if a File Exists

If you are working with PHP and need a way to confirm if something is indeed a file, the is_file function will…

PHP Boolean: Assigning True or False to a Variable

You can assign the boolean data type to PHP variables or use it as a direct value. This enables you…

PHP Elvis Operator: How (?:) Works with Examples

The Elvis operator in PHP is a shorthand version of the ternary operator. PHP allows developers to use this conditional…

PHP strtoupper Function: Convert Strings to Uppercase

Use strtoupper() function when you want to change all letters in a string to uppercase in PHP. It works with…

PHP implode Function: Join Array Values into a String

The implode function in PHP appeared to solve a common need—joining array elements into a single string. It helps format…

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.