为了账号安全,请及时绑定邮箱和手机立即绑定

php 中的 foreach(菜单和子菜单)

php 中的 foreach(菜单和子菜单)

PHP
MMMHUHU 2023-08-26 17:47:04
我已经通过 oop php 创建了一个菜单和子菜单,但 foreach 循环有问题。我想要实现的目标是让子菜单数组仅在菜单数组的第三个元素中循环!我希望我描述得很好!这是 foreach 代码:<ul id="navigation">    <?php    $HeaderMenuOBJ = new Headermenu();    $HeaderMenu = $HeaderMenuOBJ->getHeaderMenu();    $SubHeaderMenu = $HeaderMenuOBJ->getHeaderSubMenu();   //var_dump($SubHeaderMenu);   //var_dump($HeaderMenu);    foreach ($HeaderMenu as $row){        ?>        <li>            <a class="active"                 href="                    <?php                     echo $row['href'];                    ?>                ">                <?php echo($row['content']);?>                            </a>            <ul class="submenu">                <?php                     foreach ($SubHeaderMenu as $row){                ?>                <li>                    <a                         href="                            <?php                                echo $row['href'];                            ?>                            ">                            <?php                                 echo $row['content'];                            ?>                    </a>                </li>                <?php                  }                ?>            </ul>      </li>        <?php    } ?></ul>这是我的 DOM(一种从数据库读取数据的控制器):class Headermenu extends dbh {    public $main_query = "SELECT * FROM `header_menu` WHERE parent is null";    public $sub_query = "SELECT * FROM `header_menu` WHERE parent = 3";    public function getHeaderMenu(){       $menu = parent::connect()->query($this->main_query);        while($row = $menu->fetch_array()){            $rows[] = $row;            /*$submenu = parent::connect()->query(str_replace("@param",$header->PK_ID,$this->sub_query));           while($sub_header = $submenu-> fetch_array()){               $header->subheaders[]=$sub_header;           }*/           //$headers[] = $header;       }       return $rows;    } 
查看完整描述

1 回答

?
倚天杖

TA贡献1828条经验 获得超3个赞

直接回答:


首先,您需要在外部和内部 foreach 中使用 2 个不同的变量:foreach ($HeaderMenu as $menuRow){现在foreach ($SubHeaderMenu as $subMenuRow){两者都有$row. 你可以这样做:if ($menuRow['id'] == 3) { foreach ($SubHeaderMenu as $subMenuRow){- 所以将内部 foreach 包含在其中,if并且仅当父 id 为 3 时才执行此操作,但最好获取所有 header_menu 行,将该数据转换为嵌套数组并按动态结构显示。


动态结构答案:


获取所有菜单项,创建菜单数组,每个菜单都会有其子菜单项。然后显示这个就很容易了。我假设只有两个级别 - 菜单和子菜单。


样本数据:


+----+-------+---------------+-----------+

| id | href  | content       | parent_id |

+----+-------+---------------+-----------+

|  1 | href1 | header1       |      NULL |

|  2 | href2 | header2       |      NULL |

|  3 | href3 | header3       |      NULL |

|  4 | href4 | subheader 1 1 |         1 |

|  5 | href5 | subheader 1 2 |         1 |

|  6 | href6 | subheader 2 1 |         2 |

+----+-------+---------------+-----------+

代码:


<?php


$mysqli = new mysqli("localhost", "zz", "zz", "zz");


// fetch all menu items

$query = 'select * from header_menu order by parent_id';

$result = $mysqli->query($query);

$data = $result->fetch_all(MYSQLI_ASSOC);


var_dump($data);


// build menu with menus and their submenus

$menu = [];

foreach ($data as $row) {

  if ($row['parent_id'] === null) {

    $menu[$row['id']] = $row;

    $menu[$row['id']]['submenus'] = [];

  } else {

    $menu[$row['parent_id']]['submenus'][] = $row;

  }

}


var_dump($menu);


// now display it in html or however you want

foreach ($menu as $item) {

  echo $item['content'].PHP_EOL;

  foreach ($item['submenus'] as $subitem) {

    echo '  '.$subitem['content'].PHP_EOL;

  }

}

$数据:


array(6) {

  [0]=>

  array(4) {

    ["id"]=>

    string(1) "1"

    ["href"]=>

    string(5) "href1"

    ["content"]=>

    string(7) "header1"

    ["parent_id"]=>

    NULL

  }

  [1]=>

  array(4) {

    ["id"]=>

    string(1) "2"

    ["href"]=>

    string(5) "href2"

    ["content"]=>

    string(7) "header2"

    ["parent_id"]=>

    NULL

  }

  [2]=>

  array(4) {

    ["id"]=>

    string(1) "3"

    ["href"]=>

    string(5) "href3"

    ["content"]=>

    string(7) "header3"

    ["parent_id"]=>

    NULL

  }

  [3]=>

  array(4) {

    ["id"]=>

    string(1) "4"

    ["href"]=>

    string(5) "href4"

    ["content"]=>

    string(13) "subheader 1 1"

    ["parent_id"]=>

    string(1) "1"

  }

  [4]=>

  array(4) {

    ["id"]=>

    string(1) "5"

    ["href"]=>

    string(5) "href5"

    ["content"]=>

    string(13) "subheader 1 2"

    ["parent_id"]=>

    string(1) "1"

  }

  [5]=>

  array(4) {

    ["id"]=>

    string(1) "6"

    ["href"]=>

    string(5) "href6"

    ["content"]=>

    string(13) "subheader 2 1"

    ["parent_id"]=>

    string(1) "2"

  }

}

$菜单:


array(3) {

  [1]=>

  array(5) {

    ["id"]=>

    string(1) "1"

    ["href"]=>

    string(5) "href1"

    ["content"]=>

    string(7) "header1"

    ["parent_id"]=>

    NULL

    ["submenus"]=>

    array(2) {

      [0]=>

      array(4) {

        ["id"]=>

        string(1) "4"

        ["href"]=>

        string(5) "href4"

        ["content"]=>

        string(13) "subheader 1 1"

        ["parent_id"]=>

        string(1) "1"

      }

      [1]=>

      array(4) {

        ["id"]=>

        string(1) "5"

        ["href"]=>

        string(5) "href5"

        ["content"]=>

        string(13) "subheader 1 2"

        ["parent_id"]=>

        string(1) "1"

      }

    }

  }

  [2]=>

  array(5) {

    ["id"]=>

    string(1) "2"

    ["href"]=>

    string(5) "href2"

    ["content"]=>

    string(7) "header2"

    ["parent_id"]=>

    NULL

    ["submenus"]=>

    array(1) {

      [0]=>

      array(4) {

        ["id"]=>

        string(1) "6"

        ["href"]=>

        string(5) "href6"

        ["content"]=>

        string(13) "subheader 2 1"

        ["parent_id"]=>

        string(1) "2"

      }

    }

  }

  [3]=>

  array(5) {

    ["id"]=>

    string(1) "3"

    ["href"]=>

    string(5) "href3"

    ["content"]=>

    string(7) "header3"

    ["parent_id"]=>

    NULL

    ["submenus"]=>

    array(0) {

    }

  }

}

菜单结构的输出:


header1

  subheader 1 1

  subheader 1 2

header2

  subheader 2 1

header3

现在您可以输出所有菜单项及其子菜单项,而不仅仅是菜单 ID 3。


查看完整回答
反对 回复 2023-08-26
  • 1 回答
  • 0 关注
  • 75 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信