Get "PHP 8 in a Nuthshell" (Soon includes PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Two ways to merge arrays in PHP

If you’re a seasoned PHP developer, you must have come across the need to merge two or more arrays in PHP. And the bona fide way to do this is by using the array_merge() function.

But, there’s one more way to do this which I’m going to discuss in this article further.

First, let’s see how you can merge two arrays in PHP using the array_merge() function.

The array_merge() function

The array_merge() function is a built-in PHP function that is used to merge one or more arrays. It takes an arbitrary number of arrays as arguments and returns a new array. The new array contains the elements of the first array followed by the elements of the second array and so on.

Here’s how you can use the array_merge() function.

$first = ['item1', 'item2'];
$second = ['item3', 'item4'];

$merged = array_merge($first, $second);

var_dump($merged);
/*
array(4) {
  [0]=>
  string(1) "item1"
  [1]=>
  string(1) "item2"
  [2]=>
  string(1) "item3"
  [3]=>
  string(1) "item4"
}
*/

As you can see, the array_merge() function returns a new array that contains the elements of the first array followed by the elements of the second array.

You may want to check PHP 8 which has additional features for arrays such as array unpacking with string keys. You can learn about all of the features in PHP 8 (8.1 and 8.2) in my compact book PHP 8 in a Nutshell.

Also, a free update with PHP 8.3 is coming later this year!

Here’s how you can merge associative arrays using the array_merge() function.

$first = ['item1' => 'value1', 'item2' => 'value2'];
$second = ['item3' => 'value3', 'item4' => 'value4'];

$merged = array_merge($first, $second);

var_dump($merged);
/*
array(4) {
  ["item1"]=>
  string(1) "value1"
  ["item2"]=>
  string(1) "value2"
  ["item3"]=>
  string(1) "value3"
  ["item4"]=>
  string(1) "value4"
}
*/

The union (+) operator

The + operator (also called the union operator) is another way to merge two or more arrays in PHP. It works similarly to the array_merge() function but not entirely the same.

It takes an arbitrary number of arrays as arguments and returns a new array. The new array contains the elements of the first array followed by the elements of the second array and so on.

Here’s how you can use the + operator.

$first = ['item1' => 'value1', 'item2' => 'value2'];
$second = ['item3' => 'value3', 'item4' => 'value4'];

$merged = $first + $second;

var_dump($merged);
/*
array(4) {
  ["item1"]=>
  string(1) "value1"
  ["item2"]=>
  string(1) "value2"
  ["item3"]=>
  string(1) "value3"
  ["item4"]=>
  string(1) "value4"
}
*/

As you can see, the + operator returns a new array that contains the elements of the first array followed by the elements of the second array.

Gotchas

On the surface, you may think the + operator works similarly to the array_merge() function and it’s true to some extent but there are some key differences here that you should know about.

Associative arrays

  • The array_merge function will overwrite the values of the first array with the values of the second array if the keys are the same.

  • Whereas, the + operator will not overwrite the values of the first array with the values of the second array if the keys are the same.

Check the example below.

$first = ['item1' => 'value1', 'item2' => 'value2'];
$second = ['item1' => 'value3', 'item4' => 'value4'];

$merged = array_merge($first, $second);
$unionMerge = $first + $second;

var_dump($merged);
/*
array(3) {
  ["item1"]=>
  string(6) "value3"
  ["item2"]=>
  string(6) "value2"
  ["item4"]=>
  string(6) "value4"
}
*/

var_dump($unionMerge);
/*
array(3) {
  ["item1"]=>
  string(6) "value1"
  ["item2"]=>
  string(6) "value2"
  ["item4"]=>
  string(6) "value4"
}
*/

As you can see, the array_merge() function overwrites the values of the first array with the values of the second array if the keys are the same. Whereas, the + operator does not overwrite the values of the first array with the values of the second array if the keys are the same.

So, if you want the same kind of behavior as the array_merge() function with the + operator in the example above, you can do it by reversing the order of the arrays like so.

$reverseUnionMerge = $second + $first;

var_dump($reverseUnionMerge);
/*
array(3) {
  ["item1"]=>
  string(6) "value3"
  ["item4"]=>
  string(6) "value4"
  ["item2"]=>
  string(6) "value2"
}
*/

Indexed arrays

When working with indexed arrays, keep in mind that the + operator completely ignores the second array and returns the first array as it is.

Whereas, the array_merge() function merges the two arrays and returns a new array.

Check the example below.

$first = [1, 2, 3];
$second = [3, 4, 5];
$third = [6];

$merged = array_merge($first, $second, $third);
$unionMerge = $first + $second + $third;

var_dump($merged);
/*
array(7) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(3)
  [4]=>
  int(4)
  [5]=>
  int(5)
  [6]=>
  int(6)
}
*/

var_dump($unionMerge);
/*
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}
*/

As you can see, the + operator completely ignores the second array and third array and returns the first array as it is. Whereas, the array_merge() function merges all of the arrays and returns a new array.

Bonus - Using the spread operator

There is, in fact, a third way to merge multiple arrays in PHP. It’s by using the spread operator and it’s available in PHP 7.4 and above.

Here’s how it works.

$first = [1, 2];
$second = [3, 4];

$spreadMerge = [...$first, ...$second];
var_dump($spreadMerge);
/*
array(4) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
}
*/

And here’s an example with associative arrays.

$first = ['item1' => 'value1', 'item2' => 'value2'];
$second = ['item1' => 'value3', 'item4' => 'value4'];

$spreadMerge = [...$first, ...$second];
var_dump($spreadMerge);
/*
array(3) {
  ["item1"]=>
  string(6) "value3"
  ["item2"]=>
  string(6) "value2"
  ["item4"]=>
  string(6) "value4"
}
*/

As you can tell, when you use the spread operator to merge multiple arrays, it essentially behaves the same way as the array_merge() function. So, in a way, it’s a shorthand for the array_merge() function.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?