PHP array_merge_recursive() Function with example

array_merge_recursive() work is utilized to blend at least two clusters, it restores another exhibit with combined components.

The main distinction among array_merge() and array_merge_recursive() work is: “It consolidates the qualities having a similar key as a cluster as opposed to abrogating the qualities.”

Syntax:

 array_merge_recursive(array1, array2,...);

Example:

 Input:
    $arr1 = array("a" => "Hello", "b" => "Hi");
    $arr2 = array("a" => "Okay!", "d" => "Nothing");

    Output:
    Array
    (
        [a] => Array  
            (
                [0] => Hello 
                [1] => Okay! 
            )

        [b] => Hi
        [d] => Nothing
    )

PHP code:

<?php
    $arr1 = array("a" => "Hello", "b" => "Hi");
    $arr2 = array("a" => "Okay!", "d" => "Nothing");
    
    //merging arrays
    $arr3 = array_merge_recursive($arr1, $arr2);
    
    //printing 
    print_r ($arr3);
?>

Output

Array
(
    [a] => Array  
        (
            [0] => Hello 
            [1] => Okay! 
        )

    [b] => Hi
    [d] => Nothing
)

Leave a Comment

error: Alert: Content is protected!!