PHP, merge keys in a multidimensional array

If I have an array that looks something like this:

Array
(
    [0] => Array
    (
        [DATA] => Array
    (
        VALUE1 = 1
        VALUE2 = 2
    )
    )
    [1] => Array
    (   
        [DATA] => Array
    (
        VALUE3 = 3
        VALUE4 = 4
    )
    )
)

And I would like to include it in this:

Array
(
    [0] => Array
    (
        [DATA] => Array
    (
        VALUE1 = 1
        VALUE2 = 2
        VALUE3 = 3
        VALUE4 = 4
    )
    )
)

Basically I want to combine all the same keys that are on the same level. What would be the best way to do this? Can array_merge functions be useful?

Hope this makes some sense and thanks in advance for any help I can get.

+5
source share
1 answer

You can use array_merge_recursiveto combine all the elements in the original array together. And since this function takes a variable number of arguments, making it cumbersome when this number is unknown at compile time, you can use the call_user_func_arrayconvenience for extra:

$result = call_user_func_array('array_merge_recursive', $array);

" " (, ), .

.

+12

All Articles