有无限深度的树木结构问题

时间:2019-07-27 05:27:28

标签: php

我有一个带有父代ID的类别列表,我想为它们创建一个树形结构。

然后我创建了这个,但是问题是它仅限于深度级别

这是类别:

$categories = 
[
    [id => 1, parent_id => 0],
    [id => 2, parent_id => 0],
    [id => 3, parent_id => 0],
    [id => 4, parent_id => 0],
    [id => 5, parent_id => 1],
    [id => 6, parent_id => 1],
    [id => 7, parent_id => 1],
    [id => 8, parent_id => 2],
    [id => 9, parent_id => 2],
    [id => 10, parent_id => 3],
    [id => 11, parent_id => 3],
    [id => 12, parent_id => 4],
    [id => 13, parent_id => 5],
    [id => 14, parent_id => 5],
    [id => 15, parent_id => 5],
    [id => 16, parent_id => 13],
    ...
];

这是我的代码,它可以正常工作:

$main = [];
foreach ($categories as $cat) {
    if ($cat->parent_id == 0) {
        $item = $cat;
        $item->subs = [];
        foreach ($all as $sub_cat) {
            if ($sub_cat->parent_id == $cat->id ) {
                $sub_item = $sub_cat;
                $sub_item->subs = [];
                foreach ($all as $sub2_cat) {
                    if ($sub2_cat->parent_id == $sub_cat->id ) {
                        $sub2_item = $sub2_cat;
                        $sub2_item->subs = [];
                        foreach ($all as $sub3_cat) {
                            if ($sub3_cat->parent_id == $sub2_cat->id ) {
                                $sub3_item = $sub3_cat;
                                $sub3_item->subs = [];
                                $sub2_item->subs[] = $sub3_item;
                            }
                        }
                       $sub_item->subs[] = $sub2_item;
                    }
                }
                $item->subs[] = $sub_item;
            }
        }
         $main[] = $item;
    }
}

输出如下:

[
   [
       id=>1,
       parent_id=>0,
       subs=>[
           [
               id=>5,
               parent_id=>1,
               subs=>[
                   [
                       id=>13,
                       parent_id=>5
                       subs=>[...]
                    ],
                    ...
               ]
           ]
       ]
    ],
    [

    ],
    ...
]

输出很好,但是我的类别数组可能会更改,并且深度可能不受限制, 因此,我需要一个递归函数来执行此操作,直到没有孩子为止。

1 个答案:

答案 0 :(得分:2)

这将为您提供帮助

function getTree(array $categories, $parentId = 0) {

    $branch = array();

    foreach ($categories as $category) 
    {
        if ($category['parent_id'] == $parentId) 
        {
            $subs = getTree($categories, $category['id']);

            if ($subs) 
            {
                $category['subs'] = $subs;
            }

            $branch[] = $category;
        }
    }

    return $branch;
}
$categories = [
    ['id' => 1, 'parent_id' => 0],
    ['id' => 2, 'parent_id' => 0],
    ['id' => 3, 'parent_id' => 0],
    ['id' => 4, 'parent_id' => 0],
    ['id' => 5, 'parent_id' => 1],
    ['id' => 6, 'parent_id' => 1],
    ['id' => 7, 'parent_id' => 1],
    ['id' => 8, 'parent_id' => 2],
    ['id' => 9, 'parent_id' => 2],
    ['id' => 10, 'parent_id' => 3],
    ['id' => 11, 'parent_id' => 3],
    ['id' => 12, 'parent_id' => 4],
    ['id' => 13, 'parent_id' => 5],
    ['id' => 14, 'parent_id' => 5],
    ['id' => 15, 'parent_id' => 5],
    ['id' => 16, 'parent_id' => 13],
];
$categoryTree = getTree($categories);

这是演示link