PHP-如何建立树形结构清单? [英] PHP - How to build tree structure list?

查看:74
本文介绍了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
        • 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. 返回找到的孩子的列表.
              1. Take the array of all elements and the id of the current parent (initially 0/nothing/null/whatever).
              2. Loop through all elements.
              3. If the parent_id of an element matches the current parent id you got in 1., the element is a child of the parent. Put it in your list of current children (here: $branch).
              4. Call the function recursively with the id of the element you have just identified in 3., i.e. find all children of that element, and add them as children element.
              5. Return your list of found children.

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

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