How to Combine and Merge Arrays in PHP

On this page we describe and demonstrate how to combine or merge two or more arrays in PHP and return a single array containing the result. We cover the array_combine and array_merge functions, and the array union operator.

array_combine

The PHP array_combine function creates a new array from two arrays that you pass as arguments to it. The first argument provides the keys for the new array while the second argument provides the values. The following example demonstrates:

$months = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 
        'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');

$ar = array_combine(range(1, 12), $months);

print_r($ar);
/* print_r output (as seen in page source view):
Array
(
    [1] => Jan
    [2] => Feb
    [3] => Mar
    [4] => Apr
    [5] => May
    [6] => June
    [7] => July
    [8] => Aug
    [9] => Sept
    [10] => Oct
    [11] => Nov
    [12] => Dec
)
*/

In the example above, we use the range function[1] to create an array of values from 1 through 12. That array is used to provide the keys while the $months array provides the values for the array returned by array_combine.

If the two arrays passed to array_combine do not have an equal number of elements, false will be returned.

array_merge

Pass any number of arrays to array_merge, and the values of each will be appended to the previous in the new array returned. Our first example demonstrates by passing two numerically indexed arrays to array_merge:

$ar = ['a', 'b', 'c'];
$ar2 = ['d', 'e', 'f'];

$ar3 = array_merge($ar, $ar2);
print_r($ar3);
/* Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
) */

The keys of the numerically indexed arrays passed to array_merge are renumbered starting from zero in the array returned. Notice that this happens even in the following example in which the keys do not match:

$ar = [0 => 'a', 1 => 'b', 2 => 'c'];
$ar2 = [5 => 'd', 8 => 'e', 3 => 'f'];

$ar3 = array_merge($ar, $ar2);
print_r($ar3);
/* Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
) */

When you pass arrays with string keys to array_merge, values in subsequent arrays will overwrite earlier values for matching string keys:

$ar = ['fruit' => 'apple', 'vegetable' => 'peas', 'grain' => 'oats'];
$ar2 = ['fruit' => 'orange', 'vegetable' => 'kale', 'nut' => 'cashew'];

$ar3 = array_merge($ar, $ar2);
print_r($ar3);
/* Array
(
    [fruit] => orange
    [vegetable] => kale
    [grain] => oats
    [nut] => cashew
) */

Array Union Operator

The array union operator (+) can also be used to merge arrays. If keys in the arrays match, values earlier in the expression will overwrite those later in the expression. This is true for both numeric and string keys.

We use the same examples to demonstrate the array union operator as we used for array_merge:

$ar = ['a', 'b', 'c'];
$ar2 = ['d', 'e', 'f'];

$ar3 = $ar + $ar2;
print_r($ar3);
/* Array
(
    [0] => a
    [1] => b
    [2] => c
) */

In the example above, the values from the the second array were not included in the result because they have the same keys as those in the first. Next let's see what the array union operator does when the numeric indexes do not match:

$ar = [0 => 'a', 1 => 'b', 2 => 'c'];
$ar2 = [5 => 'd', 8 => 'e', 3 => 'f'];

$ar3 = $ar + $ar2;
print_r($ar3);
/* Array
(
    [0] => a
    [1] => b
    [2] => c
    [5] => d
    [8] => e
    [3] => f
) */

Notice that, contrary to array_merge, the indexes were not renumbered in the array union operator's result for the example above.

Our next example applies the array union operator to arrays with string keys, some of which match and some do not:

$ar = ['fruit' => 'apple', 'vegetable' => 'peas', 'grain' => 'oats'];
$ar2 = ['fruit' => 'orange', 'vegetable' => 'kale', 'nut' => 'cashew'];

$ar3 = $ar + $ar2;
print_r($ar3);
/* Array
(
    [fruit] => apple
    [vegetable] => peas
    [grain] => oats
    [nut] => cashew
) */

As you can see in the example above, when string keys match, the values from the first array are used in the result returned by the array union operator,

Back to top


  1. Find out more about the range function along with other PHP functions that fill an array with values. ^