创建父子数组PHP

时间:2016-01-30 17:46:24

标签: php

我有这个扁平数组,我试图变成一个父子数组:

Array
(
    [0] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_cze7b2e
        )

    [1] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_czeg8ay
        )

    [2] => Array
        (
            [parent_id] => t1_czeg8ay
            [id] => t1_czet481
        )

    [3] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_cze4whs
        )

    [4] => Array
        (
            [parent_id] => t1_cze4whs
            [id] => t1_cze9v0o
        )

    [5] => Array
        (
            [parent_id] => t1_cze9v0o
            [id] => t1_czec4vd
        )

    [6] => Array
        (
            [parent_id] => t1_czec4vd
            [id] => t1_czep9e2
        )

    [7] => Array
        (
            [parent_id] => t1_czep9e2
            [id] => t1_czf2k8e
        )

    [8] => Array
        (
            [parent_id] => t1_czec4vd
            [id] => t1_czeixa7
        )

    [9] => Array
        (
            [parent_id] => t1_cze9v0o
            [id] => t1_czecx26
        )

    [10] => Array
        (
            [parent_id] => t1_czecx26
            [id] => t1_czeenuz
        )

    [11] => Array
        (
            [parent_id] => t1_czeenuz
            [id] => t1_czeftkf
        )

    [12] => Array
        (
            [parent_id] => t1_czecx26
            [id] => t1_czehjrz
        )

    [13] => Array
        (
            [parent_id] => t1_czecx26
            [id] => t1_czeien4
        )

    [14] => Array
        (
            [parent_id] => t1_cze9v0o
            [id] => t1_czebugl
        )

    [15] => Array
        (
            [parent_id] => t1_cze4whs
            [id] => t1_cze9hvb
        )

    [16] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_cze3vku
        )

    [17] => Array
        (
            [parent_id] => t1_cze3vku
            [id] => t1_czemk0g
        )

    [18] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_czeedh9
        )

    [19] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_czeenjx
        )

    [20] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_cze5r5u
        )

    [21] => Array
        (
            [parent_id] => t1_cze5r5u
            [id] => t1_czefle4
        )

    [22] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_cze8oht
        )

    [23] => Array
        (
            [parent_id] => t1_cze8oht
            [id] => t1_czelwvs
        )

    [24] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_cze5cs6
        )

    [25] => Array
        (
            [parent_id] => t1_cze5cs6
            [id] => t1_czefydi
        )

    [26] => Array
        (
            [parent_id] => t1_cze5cs6
            [id] => t1_czedpml
        )

    [27] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_cze42aq
        )

    [28] => Array
        (
            [parent_id] => t1_cze42aq
            [id] => t1_cze8iei
        )

    [29] => Array
        (
            [parent_id] => t1_cze8iei
            [id] => t1_czebkgk
        )
    [30] => Array
        (
            [parent_id] => t1_czebkgk
            [id] => t1_czedid2
        )
    [31] => Array
        (
            [parent_id] => t1_cze8iei
            [id] => t1_czebqgn
        )
    [32] => Array
        (
            [parent_id] => t1_cze42aq
            [id] => t1_cze77xr
        )
    [33] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_czen20j
        )
)

这是我的递归函数,它映射id并使其成为父子数组:

function buildTree(array $elements, $parentId = 0) {
    $branch = array();

    foreach ($elements as $element) {
        if ($element['parent_id'] == $parentId) {
            $children = buildTree($elements, $element['id']);
            if ($children) {
                $element['children'] = $children;
            }
            $branch[] = $element;
        }
    }

    return $branch;
}

以下是我从上述函数获得的输出:

Array
(
    [0] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_cze7b2e
        )

    [1] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_czeg8ay
            [children] => Array
                (
                    [0] => Array
                        (
                            [parent_id] => t1_czeg8ay
                            [id] => t1_czet481
                        )

                )

        )

    [2] => Array
        (
            [parent_id] => t1_czeg8ay
            [id] => t1_czet481
        )

    [3] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_cze4whs
            [children] => Array
                (
                    [0] => Array
                        (
                            [parent_id] => t1_cze4whs
                            [id] => t1_cze9v0o
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [parent_id] => t1_cze9v0o
                                            [id] => t1_czec4vd
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [parent_id] => t1_czec4vd
                                                            [id] => t1_czep9e2
                                                            [children] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [parent_id] => t1_czep9e2
                                                                            [id] => t1_czf2k8e
                                                                        )

                                                                )

                                                        )

                                                    [1] => Array
                                                        (
                                                            [parent_id] => t1_czec4vd
                                                            [id] => t1_czeixa7
                                                        )

                                                )

                                        )

                                    [1] => Array
                                        (
                                            [parent_id] => t1_cze9v0o
                                            [id] => t1_czecx26
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [parent_id] => t1_czecx26
                                                            [id] => t1_czeenuz
                                                            [children] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [parent_id] => t1_czeenuz
                                                                            [id] => t1_czeftkf
                                                                        )

                                                                )

                                                        )

                                                    [1] => Array
                                                        (
                                                            [parent_id] => t1_czecx26
                                                            [id] => t1_czehjrz
                                                        )

                                                    [2] => Array
                                                        (
                                                            [parent_id] => t1_czecx26
                                                            [id] => t1_czeien4
                                                        )

                                                )

                                        )

                                    [2] => Array
                                        (
                                            [parent_id] => t1_cze9v0o
                                            [id] => t1_czebugl
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [parent_id] => t1_cze4whs
                            [id] => t1_cze9hvb
                        )

                )

        )

    [4] => Array
        (
            [parent_id] => t1_cze4whs
            [id] => t1_cze9v0o
            [children] => Array
                (
                    [0] => Array
                        (
                            [parent_id] => t1_cze9v0o
                            [id] => t1_czec4vd
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [parent_id] => t1_czec4vd
                                            [id] => t1_czep9e2
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [parent_id] => t1_czep9e2
                                                            [id] => t1_czf2k8e
                                                        )

                                                )

                                        )

                                    [1] => Array
                                        (
                                            [parent_id] => t1_czec4vd
                                            [id] => t1_czeixa7
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [parent_id] => t1_cze9v0o
                            [id] => t1_czecx26
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [parent_id] => t1_czecx26
                                            [id] => t1_czeenuz
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [parent_id] => t1_czeenuz
                                                            [id] => t1_czeftkf
                                                        )

                                                )

                                        )

                                    [1] => Array
                                        (
                                            [parent_id] => t1_czecx26
                                            [id] => t1_czehjrz
                                        )

                                    [2] => Array
                                        (
                                            [parent_id] => t1_czecx26
                                            [id] => t1_czeien4
                                        )

                                )

                        )

                    [2] => Array
                        (
                            [parent_id] => t1_cze9v0o
                            [id] => t1_czebugl
                        )

                )

        )

    [5] => Array
        (
            [parent_id] => t1_cze9v0o
            [id] => t1_czec4vd
            [children] => Array
                (
                    [0] => Array
                        (
                            [parent_id] => t1_czec4vd
                            [id] => t1_czep9e2
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [parent_id] => t1_czep9e2
                                            [id] => t1_czf2k8e
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [parent_id] => t1_czec4vd
                            [id] => t1_czeixa7
                        )

                )

        )

    [6] => Array
        (
            [parent_id] => t1_czec4vd
            [id] => t1_czep9e2
            [children] => Array
                (
                    [0] => Array
                        (
                            [parent_id] => t1_czep9e2
                            [id] => t1_czf2k8e
                        )

                )

        )

    [7] => Array
        (
            [parent_id] => t1_czep9e2
            [id] => t1_czf2k8e
        )

    [8] => Array
        (
            [parent_id] => t1_czec4vd
            [id] => t1_czeixa7
        )

    [9] => Array
        (
            [parent_id] => t1_cze9v0o
            [id] => t1_czecx26
            [children] => Array
                (
                    [0] => Array
                        (
                            [parent_id] => t1_czecx26
                            [id] => t1_czeenuz
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [parent_id] => t1_czeenuz
                                            [id] => t1_czeftkf
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [parent_id] => t1_czecx26
                            [id] => t1_czehjrz
                        )

                    [2] => Array
                        (
                            [parent_id] => t1_czecx26
                            [id] => t1_czeien4
                        )

                )

        )

    [10] => Array
        (
            [parent_id] => t1_czecx26
            [id] => t1_czeenuz
            [children] => Array
                (
                    [0] => Array
                        (
                            [parent_id] => t1_czeenuz
                            [id] => t1_czeftkf
                        )

                )

        )

    [11] => Array
        (
            [parent_id] => t1_czeenuz
            [id] => t1_czeftkf
        )

    [12] => Array
        (
            [parent_id] => t1_czecx26
            [id] => t1_czehjrz
        )

    [13] => Array
        (
            [parent_id] => t1_czecx26
            [id] => t1_czeien4
        )

    [14] => Array
        (
            [parent_id] => t1_cze9v0o
            [id] => t1_czebugl
        )

    [15] => Array
        (
            [parent_id] => t1_cze4whs
            [id] => t1_cze9hvb
        )

    [16] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_cze3vku
            [children] => Array
                (
                    [0] => Array
                        (
                            [parent_id] => t1_cze3vku
                            [id] => t1_czemk0g
                        )

                )

        )

    [17] => Array
        (
            [parent_id] => t1_cze3vku
            [id] => t1_czemk0g
        )

    [18] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_czeedh9
        )

    [19] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_czeenjx
        )

    [20] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_cze5r5u
            [children] => Array
                (
                    [0] => Array
                        (
                            [parent_id] => t1_cze5r5u
                            [id] => t1_czefle4
                        )

                )

        )

    [21] => Array
        (
            [parent_id] => t1_cze5r5u
            [id] => t1_czefle4
        )

    [22] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_cze8oht
            [children] => Array
                (
                    [0] => Array
                        (
                            [parent_id] => t1_cze8oht
                            [id] => t1_czelwvs
                        )

                )

        )

    [23] => Array
        (
            [parent_id] => t1_cze8oht
            [id] => t1_czelwvs
        )

    [24] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_cze5cs6
            [children] => Array
                (
                    [0] => Array
                        (
                            [parent_id] => t1_cze5cs6
                            [id] => t1_czefydi
                        )

                    [1] => Array
                        (
                            [parent_id] => t1_cze5cs6
                            [id] => t1_czedpml
                        )

                )

        )

    [25] => Array
        (
            [parent_id] => t1_cze5cs6
            [id] => t1_czefydi
        )

    [26] => Array
        (
            [parent_id] => t1_cze5cs6
            [id] => t1_czedpml
        )

    [27] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_cze42aq
            [children] => Array
                (
                    [0] => Array
                        (
                            [parent_id] => t1_cze42aq
                            [id] => t1_cze8iei
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [parent_id] => t1_cze8iei
                                            [id] => t1_czebkgk
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [parent_id] => t1_czebkgk
                                                            [id] => t1_czedid2
                                                        )

                                                )

                                        )

                                    [1] => Array
                                        (
                                            [parent_id] => t1_cze8iei
                                            [id] => t1_czebqgn
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [parent_id] => t1_cze42aq
                            [id] => t1_cze77xr
                        )

                )

        )

    [28] => Array
        (
            [parent_id] => t1_cze42aq
            [id] => t1_cze8iei
            [children] => Array
                (
                    [0] => Array
                        (
                            [parent_id] => t1_cze8iei
                            [id] => t1_czebkgk
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [parent_id] => t1_czebkgk
                                            [id] => t1_czedid2
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [parent_id] => t1_cze8iei
                            [id] => t1_czebqgn
                        )

                )

        )

    [29] => Array
        (
            [parent_id] => t1_cze8iei
            [id] => t1_czebkgk
            [children] => Array
                (
                    [0] => Array
                        (
                            [parent_id] => t1_czebkgk
                            [id] => t1_czedid2
                        )

                )

        )

    [30] => Array
        (
            [parent_id] => t1_czebkgk
            [id] => t1_czedid2
        )

    [31] => Array
        (
            [parent_id] => t1_cze8iei
            [id] => t1_czebqgn
        )

    [32] => Array
        (
            [parent_id] => t1_cze42aq
            [id] => t1_cze77xr
        )

    [33] => Array
        (
            [parent_id] => t3_42yrg7
            [id] => t1_czen20j
        )

)

虽然它的第一部分是正确的,但它有时会在第一次正确映射后将子数组标记为父类。为什么有些子数组被映射为父数组?<​​/ p>

编辑:我是个白痴,当我打电话给buildTree()时,我没有传入一个parentId,只是将该值保留为零。

1 个答案:

答案 0 :(得分:1)

我不知道你想要什么,但我已将我的代码添加到数组中的父和子分开。请查看以下代码:

function buildTree($elements = array()) {
    $branch = array();
    if(!empty($elements))
    {
        foreach ($elements as $element) {
            if(!isset($branch[$element['parent_id']]))
            {
                $branch['parent_id'][]=$element['parent_id'];
                $branch[$element['parent_id']]=array();
                $branch[$element['parent_id']]['children'][]=$element['id'];
            }
            else
            {
                $branch[$element['parent_id']]['children'][]=$element['id'];
            }
        }
    }
    return $branch;
}
$arr_rtn = buildTree($arr);