PHP:合并数组而不替换数据

时间:2016-03-26 13:57:13

标签: php arrays replace merge

给出了多个数组,结构如下(print_r):

Array
(
    [name] => Top Level
    [parent] => null
    [children] => Array
        (
            [name] => Level 2: A
            [parent] => Top Level
            [children] => Array
                (
                    [name] => Son of A
                    [parent] => Level 2: A
                )

        )

)

Array
(
    [name] => Top Level
    [parent] => null
    [children] => Array
        (
            [name] => Level 2: A
            [parent] => Top Level
            [children] => Array
                (
                    [name] => Daughter of A
                    [parent] => Level 2: A
                )

        )

)

创建数组的代码:

$array1 = array("name" => "Top Level", "parent" => "null", "children" => array("name" => "Level 2: A", "parent" => "Top Level", "children" => array("name" => "Son of A", "parent" => "Level 2: A")));
$array2 = array("name" => "Top Level", "parent" => "null", "children" => array("name" => "Level 2: A", "parent" => "Top Level", "children" => array("name" => "Daughter of A", "parent" => "Level 2: A")));

我想合并它们。我已经尝试了array_replace_recursivearray_merge_recursive,但两人都没有给我估计结果。 所需的结果如下所示:

Array
(
    [name] => Top Level
    [parent] => null
    [children] => Array
        (
            [name] => Level 2: A
            [parent] => Top Level
            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => Son of A
                            [parent] => Level 2: A
                        )

                    [1] => Array
                        (
                            [name] => Daughter of A
                            [parent] => Level 2: A
                        )

                )

        )

)

有关如何实现这一点的任何建议?谢谢!

0 个答案:

没有答案