无法在php中合并嵌套数组数据

I try to merge the data's in the arrays in 'c' and 'a' inside MYDATA but is it possible? Some examples and tips would be helpful! I would love to hear from you!

[0] => Array
    (
        [MYDATA] => Array
            (
                [id] => 79
                [my_birth_day] => 1990-06-20
                [my_address] => 400
                [my_age] => 26
                [my_name] => Joy
                [my_id] => 1
                [created] => 2017-06-19 15:39:44
            )

        [c] => Array
            (
                [my_test] => math
            )

        [a] => Array
            (
                [my_date] => 2017-08-13
            )

    )

I would want the result to be like

[MYDATA] => Array
            (
                [id] => 79
                [my_birth_day] => 1990-06-20
                [my_address] => 400
                [my_age] => 26
                [my_name] => Joy
                [my_id] => 1
                [created] => 2017-06-19 15:39:44
                [my_test] => math
                [my_date] => 2017-08-13

The function needed is array_merge(), it takes an unlimited number of parameters, the order of the parameters dictates the order in which the arrays are merged, in this case it would not matter, but say you had matching keys, say two 'my_address' keys the value from the last array defining this key will be returned as a result as the function call.

See the docs here: http://php.net/manual/en/function.array-merge.php

Merge the inner arrays, assuming your outermost array is called $array you can try the following:

<?php 

$array = array(
    'MYDATA' => array(
        'id' => '79',
            'my_birth_day' => '1990-06-20',
            'my_address' => '400',
            'my_age' => '26',
            'my_name' => 'Joy',
            'my_id' => '1',
            'created' => '2017-06-19 15:39:44'
    ),
    'c' => array(
        'my_test' => 'math'
    ),
    'a' => array(
        'my_date' => '2017-08-13'
    )
 );

 $mergedMyData = array_merge($array['MYDATA'], $array['c'], $array['a']);

 //output the data
 print_r($mergedMyData);

 ?>

hope this helps

Just merge the elements one by one:

<?php
$input = [
    'MYDATA' => [
        'id' => 79,
        'my_birth_day' => '1990-06-20',
        'my_address' => 400,
        'my_age' => 26,
        'my_name' => 'Joy',
        'my_id' => 1,
        'created' => '2017-06-19 15:39:44'
    ],
    'c' => [
        'my_test' => 'math'
    ],
    'a' => [
        'my_date' => '2017-08-13'
    ]
];
$output = [];
array_walk($input, function($element) use (&$output) {
    $output = array_merge($output, $element);
});
print_r($output);

The order in which you iterate over the input array determines which entries will win in case you have key collisions.


The output obviously is:

Array
(
    [id] => 79
    [my_birth_day] => 1990-06-20
    [my_address] => 400
    [my_age] => 26
    [my_name] => Joy
    [my_id] => 1
    [created] => 2017-06-19 15:39:44
    [my_test] => math
    [my_date] => 2017-08-13
)