0

很抱歉这么长的描述,但我在一天多的时间里一直在努力合并多个多维关联数组。任何人都可以建议实现这一目标的优化方法。我想根据所需的结果创建 HTML 列表。请参阅当前列表的附件图片。但我想把孩子合并成共同的父母。

阵列 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
            )
        )
    )
)

阵列 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
            )
        )
    )
)

我希望结果保持公共数组不变,并在同一级别上创建另一个子数组,如下所示。

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
                )
            )
        )
    )
)

[在此处输入图片描述][1]

[1]:当前列表:https ://i.stack.imgur.com/p4F0X.png

4

1 回答 1

0

array_merge_recursive应该可以帮助你。

一个例子是:

<?php
$arr1['foo']['bar'] = 'foobar';
$arr2['foo']['bar'] = 'barfoo';

print_r(array_merge_recursive($arr1, $arr2));

输出:

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

        )

)
于 2020-06-07T23:23:28.413 回答