How to Combine Two Arrays in PHP

Minahil Noor Mar 13, 2025 PHP PHP Array
  1. Using array_merge() Function
  2. Using the Sum Operator
  3. Conclusion
  4. FAQ
How to Combine Two Arrays in PHP

Combining arrays in PHP is a fundamental skill that every developer should master. Whether you’re working on a simple project or a complex application, knowing how to merge two arrays can save you time and enhance your code’s efficiency. In this article, we will explore two primary methods to combine arrays: the array_merge() function and the sum operator. By the end of this guide, you’ll have a solid understanding of how to implement both techniques in your PHP code.

Understanding how to merge arrays can help you manage data more effectively. Imagine you have two sets of data that you need to combine for processing. Using PHP’s built-in functions, you can easily achieve this without writing extensive code. Let’s dive into the methods you can use to combine arrays in PHP.

Using array_merge() Function

The array_merge() function is one of the most straightforward ways to combine two arrays in PHP. This function takes multiple arrays as arguments and merges them into one. When using array_merge(), if the arrays contain numeric keys, the function will reindex the resulting array. If the arrays have string keys, the values will be overwritten by the last array’s values.

Here is a simple example of how to use array_merge():

$array1 = ['apple', 'banana'];
$array2 = ['orange', 'grape'];

$mergedArray = array_merge($array1, $array2);

print_r($mergedArray);

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => grape
)

In this example, we have two arrays, $array1 and $array2. By calling array_merge($array1, $array2), we create a new array called $mergedArray that contains all the elements from both arrays. The output shows that the elements from both arrays are now part of a single array. This method is particularly useful when you need to combine lists or datasets without worrying about the keys.

Using the Sum Operator

Another method to combine arrays in PHP is by using the sum operator (+). This method is slightly different from array_merge(), as it preserves the keys of the original arrays. If the arrays have overlapping keys, the values from the first array will be retained, and those from the second array will be ignored.

Here’s how you can use the sum operator to combine two arrays:

$array1 = ['a' => 'apple', 'b' => 'banana'];
$array2 = ['b' => 'orange', 'c' => 'grape'];

$combinedArray = $array1 + $array2;

print_r($combinedArray);

Output:

Array
(
    [a] => apple
    [b] => banana
    [c] => grape
)

In this example, $array1 contains two elements with keys ‘a’ and ‘b’, while $array2 has the same key ‘b’ but with a different value. When we use the sum operator to combine them, the resulting array $combinedArray retains the value of ‘b’ from $array1, ignoring the value from $array2. This method is beneficial when you want to maintain the integrity of your keys while merging arrays.

Conclusion

Combining arrays in PHP is a simple yet essential task that can enhance your coding efficiency. Whether you choose to use the array_merge() function for a straightforward merge or the sum operator to preserve keys, both methods have their unique advantages. By mastering these techniques, you can handle data more effectively in your PHP applications. Remember to choose the method that best suits your specific needs and the structure of your data.

FAQ

  1. What is the difference between array_merge() and the sum operator?
    array_merge() reindexes numeric keys and overwrites values with the same string keys, while the sum operator keeps the original keys and retains the first value for overlapping keys.

  2. Can I merge more than two arrays using array_merge()?
    Yes, array_merge() can take multiple arrays as arguments, allowing you to merge more than two arrays at once.

  3. What happens if I use array_merge() with empty arrays?
    If you use array_merge() with empty arrays, the result will simply be the non-empty arrays combined. An empty array will not affect the output.

  4. Are there performance differences between array_merge() and the sum operator?
    Generally, the performance difference is negligible for small arrays. However, for larger datasets, the sum operator may be slightly faster as it does not create a new array with reindexed keys.

  5. Can I combine arrays with different data types?
    Yes, PHP allows you to combine arrays containing different data types, including strings, integers, and objects, using either method.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe

Related Article - PHP Array