3

I need to merge those two arrays ($array2 in $array1):

$array1 = array(
    'data' => array(
        array(
            'raw' => array(
                'key1' => array('value1', 'value1_1'),
                'key3' => 'value3',
            ),
        ),
        array(
            'raw' => array(
                'key1' => array('value10', 'value10_1'),
                'key3' => 'value30',
            ),
        ),
        ...
    ),
);

$array2 = array(
    'data' => array(
       array(
           'raw' => array(
               'key1' => 'value1_2',
               'key2' => 'value2',
           )
       )
    )
);

Expected result :

array(
    'data' => array(
        array(
            'raw' => array(
                'key1' => array('value1', 'value1_1', 'value1_2'),
                'key2' => 'value2',
                'key3' => 'value3',
            ),
        ),
        array(
            'raw' => array(
                'key1' => array('value10', 'value10_1'),
                'key3' => 'value30',
            ),
        ),
    ),
);

When I'm using array_merge_recursive to merge $array2 in $array1['data'][0], I've got the following result:

$array1 = array(
    'data' => array(
        array(
            'raw' => array(
                'key1' => array('value1', 'value1_1'),
                'key3' => 'value3',
            ),
        ),
        array(
            'raw' => array(
                'key1' => array('value10', 'value10_1'),
                'key3' => 'value30',
            ),
        ),
        array(
           'raw' => array(
               'key1' => 'value1_2',
               'key2' => 'value2',
           )
       )
    ),
);

When I'm using array_replace_recursive, I've got the following result:

$array1 = array(
    'data' => array(
        array(
            'raw' => array(
                'key1' => 'value1_2',
                'key2' => 'value2',
                'key3' => 'value3',
            ),
        ),
        array(
            'raw' => array(
                'key1' => array('value10', 'value10_1'),
                'key3' => 'value30',
            ),
        ),
    ),
);

I'm looking for the simplest way to get the expected result. The arrays can have an unknown deph data structure and it can have more than one indexed subarray. It seems I need a combinaison between array_merge_recursive and array_replace_recursive

Thanks a lot for your answers :)

4
  • 1
    Do you NEED recursion? Is your data structure of unknown depth? Might the data subarray have more than one indexed subarray? If not, then I would argue that you should change your structure. ...Indexed arrays can really foul things up sometimes. Commented May 16, 2018 at 12:39
  • 1
    Yes it is an unknow deph data structure. Indeed, the array can have more than one indexed subarray.... I will edit my answer to be more explicit. Commented May 16, 2018 at 12:58
  • Please provide sample input that clarifies the complexity of your data. Also, post your actual/complete/best coding attempt so that this isn't closed as Too Broad. Commented May 16, 2018 at 13:04
  • I edited my answer. Tell me if you need more info. Thanks a lot :-) Commented May 16, 2018 at 13:14

1 Answer 1

2

Just needing a bit of preparation first, to weed out that unwanted indexed subarray.

Code: (Demo)

$array1['data']=current($array1['data']); // repair
$array2['data']=current($array2['data']); // repair
var_export(array_merge_recursive($array1, $array2));  // merge as intended

Output:

array (
  'data' => 
  array (
    'raw' => 
    array (
      'key1' => 
      array (
        0 => 'value1',
        1 => 'value1_1',
        2 => 'value1_2',
      ),
      'key3' => 'value3',
      'key2' => 'value2',
    ),
  ),
)

After question update to express a more complex data structure...

Code: (Demo)

foreach ($array1['data'] as $set) {
    foreach ($set['raw'] as $k => $v) {
        if (!isset($result['data']['raw'][$k])) {
            $result['data']['raw'][$k] = $v;
        }else {
            $result['data']['raw'][$k] = array_merge((array)$result['data']['raw'][$k],(array)$v);
        }
    }
}
foreach ($array2['data'] as $set) {
    foreach ($set['raw'] as $k => $v) {
        if (!isset($result['data']['raw'][$k])) {
            $result['data']['raw'][$k] = $v;
        }else{
            $result['data']['raw'][$k] = array_merge((array)$result['data']['raw'][$k],(array)$v);
        }
    }
}
var_export($result);
Sign up to request clarification or add additional context in comments.

4 Comments

this is only okay for the specific sample data, I suspect the aswer should be more robust
Sure. I hope the OP clarifies. I can only build a solution for what is provided. Otherwise it is a rabbit hole of possibilities and fringe cases.
@BwaBwa Please check my new snippet on your actual project data and let me know if it breaks. ...and if it does, please offer a minimal input array that causes the issue and I'll try again.
@mickmackusa Wow it works as expected ! I knew that will not be simple. Big thanks !! If I find a simplest way; I will edit your answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.