PHP - 如何构建树结构列表? [英] PHP - How to build tree structure list?

查看:24
本文介绍了PHP - 如何构建树结构列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的问题是,我想构建这两个表的树:

So, my problem is, that I want to build a tree of these 2 tables:

Parent table:
+-------+---------------+
| pr_id |  parent_name  |
+-------+---------------+
|   1   |       p       |
|   2   |      p_0      | 
|   3   |     p_0_1     | 
|   4   |       q       | 
+-------+---------------+

Child table:
+-------+---------------+---------------------------+
| ch_id |     pr_id     |        child_name         |
+-------+---------------+---------------------------+
|   1   |       1       |            p_0            |
|   2   |       1       |            p_1            |
|   3   |       2       |           p_0_0           |
|   4   |       2       |           p_0_1           |
|   5   |       3       |          p_0_1_0          |
|   6   |       3       |          p_0_1_1          |
|   7   |       4       |            q_0            |
|   8   |       4       |            q_1            |
+-------+---------------+---------------------------+

树应该看起来像:

  • p
    • p_0
      • p_0_0
      • p_0_1
        • p_0_1_0
        • p_0_1_1

        有人可以帮我解决递归问题吗??

        Can anybody help me out with a recursive solution??

        推荐答案

        您不需要为此在数据库中创建 2 个表,您可以仅从一个表中进行如下维护

        You do not need to create 2 tables in the database for this you can maintain it like below from one table only

        +-------+---------------+---------------------------+
        |   id  |   parent_id   |           title           |
        +-------+---------------+---------------------------+
        |   1   |       0       |   Parent Page             |
        |   2   |       1       |   Sub Page                |
        |   3   |       2       |   Sub Sub Page            |
        |   4   |       0       |   Another Parent Page     |
        +-------+---------------+---------------------------+
        

        生成的数组就像

        Array
        (
            [0] => Array
                (
                    [id] => 1
                    [parent_id] => 0
                    [title] => Parent Page
                    [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 2
                                            [parent_id] => 1
                                            [title] => Sub Page
                                            [children] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [id] => 3
                                                                    [parent_id] => 1
                                                                    [title] => Sub Sub Page
                                                                )
                                                        )
                                        )
                                )
                )
            [1] => Array
                (
                    [id] => 4
                    [parent_id] => 0
                    [title] => Another Parent Page
                )
        )
        

        你需要使用下面的递归函数来实现它

        You need to use the below recursive function to achieve it

        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;
        }
        
        $tree = buildTree($rows);
        

        算法非常简单:

        1. 取所有元素的数组和当前父元素的id(最初为 0/无/空/随便什么).
        2. 遍历所有元素.
        3. 如果元素的 parent_id 与您在 1. 中获得的当前父 ID 匹配,则该元素是父元素的子元素.把它放在你的清单中当前孩子的数量(此处:$branch).
        4. 使用您刚刚在 3. 中标识的元素的 id 递归调用该函数,即查找该元素的所有子元素,并将它们添加为子元素.
        5. 返回您找到的孩子的名单.

        这篇关于PHP - 如何构建树结构列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆