<small id='XL1Fz'></small><noframes id='XL1Fz'>

      • <bdo id='XL1Fz'></bdo><ul id='XL1Fz'></ul>

    1. <tfoot id='XL1Fz'></tfoot>
      1. <i id='XL1Fz'><tr id='XL1Fz'><dt id='XL1Fz'><q id='XL1Fz'><span id='XL1Fz'><b id='XL1Fz'><form id='XL1Fz'><ins id='XL1Fz'></ins><ul id='XL1Fz'></ul><sub id='XL1Fz'></sub></form><legend id='XL1Fz'></legend><bdo id='XL1Fz'><pre id='XL1Fz'><center id='XL1Fz'></center></pre></bdo></b><th id='XL1Fz'></th></span></q></dt></tr></i><div id='XL1Fz'><tfoot id='XL1Fz'></tfoot><dl id='XL1Fz'><fieldset id='XL1Fz'></fieldset></dl></div>
        <legend id='XL1Fz'><style id='XL1Fz'><dir id='XL1Fz'><q id='XL1Fz'></q></dir></style></legend>

        PHP 多级菜单

        时间:2023-10-11

        <small id='x8XiS'></small><noframes id='x8XiS'>

        <tfoot id='x8XiS'></tfoot>
        • <legend id='x8XiS'><style id='x8XiS'><dir id='x8XiS'><q id='x8XiS'></q></dir></style></legend>

                <bdo id='x8XiS'></bdo><ul id='x8XiS'></ul>
                  <tbody id='x8XiS'></tbody>

                <i id='x8XiS'><tr id='x8XiS'><dt id='x8XiS'><q id='x8XiS'><span id='x8XiS'><b id='x8XiS'><form id='x8XiS'><ins id='x8XiS'></ins><ul id='x8XiS'></ul><sub id='x8XiS'></sub></form><legend id='x8XiS'></legend><bdo id='x8XiS'><pre id='x8XiS'><center id='x8XiS'></center></pre></bdo></b><th id='x8XiS'></th></span></q></dt></tr></i><div id='x8XiS'><tfoot id='x8XiS'></tfoot><dl id='x8XiS'><fieldset id='x8XiS'></fieldset></dl></div>

                  本文介绍了PHP 多级菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个这样的主题表:

                  I have a subject table like this:

                  id
                  title
                  parent_id
                  full_path
                  

                  full_path 用于以递归方式查找父级.像这样:

                  full_path is for finding parent as recursive. Like this:

                  +----+-----------+-----------+-----------+
                  | id | title     | full_path | parent_id |
                  +----+-----------+-----------+-----------+
                  | 40 | home      | 40        |         0 |
                  | 41 | myhome1   | 41        |         0 |
                  | 42 | ****      | 40-42     |        40 |
                  | 43 | *****     | 41-43     |        41 |
                  | 44 | ***       | 44        |         0 |
                  | 45 | ****      | 45        |         0 |
                  | 46 | *****     | 46        |         0 |
                  | 49 | ******    | 49        |         0 |
                  | 50 | **** **   | 40-42-50  |        42 |
                  | 51 | **** **   | 40-42-51  |        42 |
                  | 52 | **** **   | 40-42-52  |        42 |
                  | 53 | *******   | 40-53     |        40 |
                  | 54 | ****      | 40-54     |        40 |
                  | 55 | ***       | 41-55     |        41 |
                  | 56 | **** **** | 40-42-56  |        42 |
                  | 57 | *******   | 44-57     |        44 |
                  +----+-----------+-----------+-----------+
                  

                  我如何获得这样的递归数组:

                  How i can get an recursive array like this:

                  array
                  (
                      40 => array
                      (
                          42 => array
                          (
                              50,51,52,etc.
                          ),
                          53,
                          54
                      )
                      41 => array
                      (
                          43,
                          55,
                      ),
                      44 => array
                      (
                          57,
                      ),
                      etc...
                  )
                  

                  我可以使用 full_path 来创建多级菜单吗?

                  Can I use full_path for create multilevel menu?

                  推荐答案

                  您可以使用下面的代码来执行此操作.请记住,这是有效的,因为您的主题数组将非常小并且发生的递归将是最小的.不要在大型数组上使用这种方法.

                  You could use the code below to do this. Keep in mind that this works because your subjects array will be very small and the recursion that happens will be minimal. Dont use this approach on large arrays.

                  <?php
                  $query = "SELECT id, parent_id FROM subjects";
                  //execute with your prefered method, eg mysqli
                  
                  $rows = array();
                  while($row = $result->fetch_array(MYSQLI_ASSOC))
                  {
                    $rows[] = $row;
                  }
                  
                  function getChildren($p) {
                    global $rows;
                    $r = array();
                    foreach($rows as $row) {
                      if ($row['parent_id']==$p) {
                        $r[$row['id']] = getChildren($row['id']);
                      }
                    }
                    return $r;
                  }
                  
                  $final = getChildren(0);
                  ?>
                  

                  这篇关于PHP 多级菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Laravel 中放置菜单逻辑的位置? 下一篇:使用 php 在菜单项上设置活动类

                  相关文章

                1. <i id='XbfPx'><tr id='XbfPx'><dt id='XbfPx'><q id='XbfPx'><span id='XbfPx'><b id='XbfPx'><form id='XbfPx'><ins id='XbfPx'></ins><ul id='XbfPx'></ul><sub id='XbfPx'></sub></form><legend id='XbfPx'></legend><bdo id='XbfPx'><pre id='XbfPx'><center id='XbfPx'></center></pre></bdo></b><th id='XbfPx'></th></span></q></dt></tr></i><div id='XbfPx'><tfoot id='XbfPx'></tfoot><dl id='XbfPx'><fieldset id='XbfPx'></fieldset></dl></div>
                    <legend id='XbfPx'><style id='XbfPx'><dir id='XbfPx'><q id='XbfPx'></q></dir></style></legend>
                      <bdo id='XbfPx'></bdo><ul id='XbfPx'></ul>
                    <tfoot id='XbfPx'></tfoot>

                    1. <small id='XbfPx'></small><noframes id='XbfPx'>