How to merge two or more arrays into one array in PHP ?

In this article you will learn how to merge two or more arrays into one array in PHP. You can easily do it after use array_merge() function to merge the elements or values of two or more arrays together into a single array.

array_merge() function

The array_merge() function merges one or more arrays into one array. Note: If two or more array elements have the same key, the last one overrides the others.

Merge two or more arrays into one array in PHP

<?php
$array1 = array("yellow", "red", "green", "orange", "purple");
$array2 = array("pink", "brown", "green", "orange", "red");
$array  = array_merge($array1, $array2);
print_r($array);
?>
The above example will output:
Array
(
    [0] => yellow
    [1] => red
    [2] => green
    [3] => orange
    [4] => purple
    [5] => pink
    [6] => brown
    [7] => green
    [8] => orange
    [9] => red
)

Leave A Reply

Your email address will not be published.