phparraysarray-mergearray-maparray-walk

PHP Merge multiple multi-dimensional, also keep unique children


Sorry for such a long description but I'm struggling with the merging of multiple multi-dimensional associative arrays for more than a day now. Can anyone please suggest the optimized way to achieve this. I want to create HTML listing based on required result. Please see attached image of current listing. But I want to merge children into common parents.

ARRAY 1:

Array
(
    [cat_id] => 1
    [parent_cat_id] => 0
    [cat_name] => Collectables
    [children] => Array
    (
        [cat_id] => 1335
        [parent_cat_id] => 1
        [cat_name] => Animal Collectables
        [children] => Array
        (
            [cat_id] => 10811
            [parent_cat_id] => 1335
            [cat_name] => Bird Collectables
            [children] => Array
            (
                [cat_id] => 37847
                [parent_cat_id] => 10811
                [cat_name] => Bluebird
            )
        )
    )
)

ARRAY 2:

Array
(
    [cat_id] => 1
    [parent_cat_id] => 0
    [cat_name] => Collectables
    [children] => Array
    (
        [cat_id] => 1335
        [parent_cat_id] => 1
        [cat_name] => Animal Collectables
        [children] => Array
        (
            [cat_id] => 10811
            [parent_cat_id] => 1335
            [cat_name] => Bird Collectables
            [children] => Array
            (
                [cat_id] => 37848
                [parent_cat_id] => 10811
                [cat_name] => Hummingbird
            )
        )
    )
)

I want the result in way that it keep common arrays as it is and create another sub-array of children on same level, like below.

Array
(
    [cat_id] => 1
    [parent_cat_id] => 0
    [cat_name] => Collectables
    [children] => Array
    (
        [cat_id] => 1335
        [parent_cat_id] => 1
        [cat_name] => Animal Collectables
        [children] => Array
        (
            [cat_id] => 10811
            [parent_cat_id] => 1335
            [cat_name] => Bird Collectables
            [children] => Array
            (
                [0] => Array(
                    [cat_id] => 37847
                    [parent_cat_id] => 10811
                    [cat_name] => Bluebird
                )
                [1] => Array(
                    [cat_id] => 37848
                    [parent_cat_id] => 10811
                    [cat_name] => Hummingbird
                )
            )
        )
    )
)

[enter image description here][1]

[1]: CURRENT LISTING: https://i.sstatic.net/p4F0X.png


Solution

  • array_merge_recursive should help you.

    An example would be:

    <?php
    $arr1['foo']['bar'] = 'foobar';
    $arr2['foo']['bar'] = 'barfoo';
    
    print_r(array_merge_recursive($arr1, $arr2));
    

    Output:

    Array
    (
        [foo] => Array
            (
                [bar] => Array
                    (
                        [0] => foobar
                        [1] => barfoo
                    )
    
            )
    
    )