cyberangles blog

PHP Merging two or more arrays using `array_merge()`

In PHP, working with arrays is a common task. One of the useful operations is merging arrays. The array_merge() function provides a straightforward way to combine two or more arrays into a single array. This blog post will explore how array_merge() works, its syntax, example usage, common practices, and best practices.

2026-07

Table of Content#

  1. Syntax of array_merge()
  2. Example Usage with Numeric Indexed Arrays
  3. Example Usage with Associative Indexed Arrays
  4. Common Practices
  5. Best Practices
  6. Reference

Syntax of array_merge()#

The basic syntax of array_merge() is as follows:

array_merge(array $array1, array $array2 [, array $... ]): array

It takes one or more arrays as arguments and returns a new array that is the combination of all the input arrays. If the input arrays have numeric keys, the values are appended in the order of the input arrays. For associative arrays (arrays with string keys), if there are duplicate keys, the value from the latter array in the argument list will overwrite the value from the earlier array.

Example Usage with Numeric Indexed Arrays#

Let's start with an example using numeric - indexed arrays.

<?php
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
$result = array_merge($array1, $array2);
print_r($result);
?>

In this example, $array1 has elements [1, 2, 3] and $array2 has elements [4, 5, 6]. When we call array_merge(), the result will be Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ). The numeric keys are simply incremented in the order of the input arrays.

Another example with more than two arrays:

<?php
$array1 = [1, 2];
$array2 = [3, 4];
$array3 = [5, 6];
$result = array_merge($array1, $array2, $array3);
print_r($result);
?>

The output will be Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )

Example Usage with Associative Indexed Arrays#

Now, let's look at an example with associative - indexed arrays.

<?php
$array1 = ['name' => 'John', 'age' => 30];
$array2 = ['age' => 31, 'city' => 'New York'];
$result = array_merge($array1, $array2);
print_r($result);
?>

Here, $array1 has keys 'name' and 'age', and $array2 has keys 'age' and 'city'. The result will be Array ( [name] => John [age] => 31 [city] => New York ). Since the 'age' key is present in both arrays, the value from $array2 (31) overwrites the value from $array1 (30).

Common Practices#

  • Combining Configuration Arrays: In a PHP application, you might have multiple configuration arrays. For example, a base configuration array and an environment - specific configuration array. You can use array_merge() to combine them.
<?php
$baseConfig = ['debug' => false, 'log_level' => 'info'];
$envConfig = ['debug' => true];
$finalConfig = array_merge($baseConfig, $envConfig);
print_r($finalConfig);
?>

The output will be Array ( [debug] => true [log_level] => info )

  • Building Data Arrays: When fetching data from different sources (e.g., database queries for different parts of a user profile), you can use array_merge() to combine the resulting arrays.

Best Practices#

  • Be Aware of Key Overwriting: As we saw in the associative array example, if you have duplicate keys, the latter value will overwrite the former. Make sure you understand the data structure and the key names when merging arrays. If you want to preserve all keys and values in case of duplicates (for example, when merging arrays of similar data but with unique identifiers), consider using array_merge_recursive() instead. However, array_merge_recursive() has its own behavior (it will create an array of values for duplicate keys).
  • Check Input Arrays: Before calling array_merge(), it's a good practice to check if the input variables are indeed arrays. You can use is_array() function.
<?php
$array1 = [1, 2];
$maybeArray = 'not an array';
if (is_array($maybeArray)) {
    $result = array_merge($array1, $maybeArray);
} else {
    // handle the error case
    echo "The second argument is not an array";
}
?>
  • Use Descriptive Variable Names: When merging arrays, use variable names that clearly indicate what each array represents. This makes the code more readable and maintainable.

Reference#