0

I have a somewhat complicated function that builds an array structure dynamically to insert fields into MongoDB. We store the structure in MySQL.

The function outputs the following array structure:

Array
(
    [address] => Array
        (
            [street] => 10 Stack St
        )

)

Array
(
    [address] => Array
        (
            [city] => Overflow Mountains
        )

)
    Array
(
    [address] => Array
        (
            [state] => CM
        )

)
Array
(
    [address] => Array
        (
            [zip] => 01010
        )

)

I need to merge these arrays so it looks like

    [address] => Array
        (
            [street] => 10 Stack St
            [city] => Overflow Mountains
            [state] => CM
            [zip] => 01010
        )

How would I go about doing this? I am using PHP.

Ben
  • 19
  • 4
  • 1
    possible duplicate of [PHP - Merge duplicate array keys in a multidimensional array](http://stackoverflow.com/questions/2165406/php-merge-duplicate-array-keys-in-a-multidimensional-array) –  Dec 05 '13 at 16:46
  • Those are 4 different arrays? – AbraCadaver Dec 05 '13 at 16:47

3 Answers3

1
$result = array_merge($array1['address'], $array2['address'], $array3['address'], $array4['address']);
Bilal
  • 2,645
  • 3
  • 28
  • 40
0

You can use array_replace_recursive().

Let your arrays be $array1, $array2, $arrray3, $array4

You can try

array_replace_recursive($array1, $array2, $array3, $array4);

Hope this helps

Sabari
  • 6,205
  • 1
  • 27
  • 36
0
array_merge($a['address'], $b['address'], $c['address'], $d['address']);
cpreid
  • 521
  • 3
  • 12