phparraysmultidimensional-arrayassociative-arraymerging-data

How to merge two associative multidimensional arrays


I have to arrays:

Array
(
    [58812b9d2e069a0680000e06] => Array
        (
            [color] => #FF00FF
        )

)

and

Array
(
    [58812b9d2e069a0680000e06] => Array
        (
            [color] => #FF0000
        )

)

Output of array_merge($arr1, $arr2) only shows $arr1 value. Could someone help me with this?

Edit 1 -I want to merge them, Even if they have same key.


Solution

  • $ar1 = array("58812b9d2e069a0680000e06" => array("color" => "#FF00FF"));
    $ar2 = array("58812b9d2e069a0680000e06" => array("color" => "#FF0000"));
    $result = array_merge_recursive($ar1, $ar2);
    print_r($result);
    

    array_merge_recursive — Merge two or more arrays recursively