PHP

PHP array_merge

The Array_merge function is an in-built function in PHP that is used to combine or merge two or more arrays. PHP array_merge() function merges arrays then after combining or merging them it gives results in a single array.

Syntax

array_merge($array1, $array2, ......, $arrayn);

Parameters

You can pass the number of arrays as much as you want to merge, as you can see in the syntax.

Example 1

<?php

$arr1=array("peter","noah");

$arr2=array("alex","john");

echo "<pre>";

print_r(array_merge($arr1,$arr2));

echo "</pre>";

?>

In the above example, we declared two arrays, and then we used the array_merge function for merging both arrays.

Output

Array
(
    [0] => peter

    [1] => noah

    [2] => alex

    [3] => john
)

As you can see from the output above the two arrays have been merged.

Example 2


<?php

$arr1=array("a"=>"red","b"=>"green");

$arr2=array("c"=>"blue","a"=>"orange");

echo "<pre>";

print_r(array_merge($arr1,$arr2));

echo "</pre>";

?>

In the above example, we declared two associative arrays and in the other associative array, we change the value of element ‘a’.

Output

Array
(
    [a] => orange

    [b] => green

    [c] => blue

)

You can see from the output the value of element ‘a’ has been updated because we changed it in the other array. When you update the value of any element in another array so after merging arrays it will show the updated value of an element.

Example 3

<?php

$arr1 = array(1, "fruit" => "kivi", 2, "cat", 3);

$arr2 = array("c", "d", "fruit" => "orange", "city" => "doha", 4, "e");

// Merging arrays together

$result = array_merge($arr1, $arr2);

echo "<pre>";

print_r($result);

echo "</pre>";

?>

In the above example, we declared two arrays of mixed values let’s check its result.

Output

Array
(

    [0] => 1

    [fruit] => orange

    [1] => 2

    [2] => cat

    [3] => 3

    [4] => c

    [5] => d

    [city] => doha

    [6] => 4

    [7] => e

)

Example 4 – Multi-Dimensional Array

<?php

$arr1 = [  
        '0'=> ['a'=>'apple','b'=>'ball'],  
        '1'=> ['c'=>'Code','d'=>'delete'],  
       ];  

$arr2 = [  
        '0'=> ['e'=>'egg','f'=>'fan'],  
        '1'=> ['g'=>'germany','h'=>'house'],  
       ];  

$res=array_merge($arr1,$arr2);

echo "<pre>";

print_r($res);

echo "</pre>";

?>

In the above example, we declare a multidimensional array let’s check how will it merge the values.

Output

Array
(
    [0] => Array
        (
            [a] => apple
            [b] => ball
        )

    [1] => Array
        (
            [c] => Code
            [d] => delete
        )

    [2] => Array
        (
            [e] => egg
            [f] => fan
        )

    [3] => Array
        (
            [g] => germany
            [h] => house
        )

)

As you can see from the output, the values of both multidimensional arrays have merged.

Conclusion

In this tutorial, we learn about the PHP array_merge function we see the definition, and then we discussed some examples.

Suggested Articles:

Leave a Comment