0

How to combine 2 arrays in PHP

To combine two arrays in PHP, do the following.

Step 1: Use the array_merge function

The array_merge function will combine 2 or more arrays and return the resulting array.

$array1 = ['foo', 'bar', 'baz'];

$array2 = ['red', 'green', 'blue'];

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

The array $combined will now be:

['foo', 'bar', 'baz', 'red', 'green', 'blue'];

Comments