• <tfoot id='jpV1K'></tfoot>

        <bdo id='jpV1K'></bdo><ul id='jpV1K'></ul>

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

        <legend id='jpV1K'><style id='jpV1K'><dir id='jpV1K'><q id='jpV1K'></q></dir></style></legend>

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

        php从多维数组动态创建导航菜单

        php create navigation menu from multidimensional array dynamically(php从多维数组动态创建导航菜单)
        • <bdo id='zxhfo'></bdo><ul id='zxhfo'></ul>

              • <small id='zxhfo'></small><noframes id='zxhfo'>

                  <tbody id='zxhfo'></tbody>
                  <tfoot id='zxhfo'></tfoot>

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

                  本文介绍了php从多维数组动态创建导航菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对此进行了研究,但无法找到确切的答案.此处与此相关的大多数问题/答案似乎尚未完成.如果有人知道类似于我的问题的最终解决方案,请指出我的方向!

                  I did research on this, and wasn't able to find an exact answer. Most of the questions/answers on here pertaining to this seem to be unfinished. If anyone knows of a finished solution similar to my question, please point me in that direction!

                  这是我的数组:

                  Array
                  (
                  ['home'] => Array
                      (
                          [0] => sub-home1
                          [1] => sub-home2
                      )
                  
                  ['about'] => Array
                      (
                          [0] => sub-about
                          ['about2'] => Array
                              (
                                  [0] => sub-sub-about
                              )
                  
                      )
                  
                  ['staff'] => Array
                      (
                          [0] => sub-staff1
                          [1] => sub-staff2
                      )
                  
                  ['contact'] => contact
                  )
                  

                  我想把它变成这样:

                  <ul>
                      <li><a href="">home<a/>
                          <ul>
                              <li><a href="">sub-home1</a></li>
                              <li><a href="">sub-home2</a></li>
                          </ul>
                      </li>
                      <li><a href="">about<a/>
                          <ul>
                              <li><a href="">sub-about</a></li>
                              <li><a href="">about2</a>
                                  <ul>
                                      <li><a href="">sub-sub-about<a/></li>
                                  </ul>
                              </li>
                          </ul>
                      </li>
                      <li><a href="">staff<a/>
                          <ul>
                              <li><a href="">sub-staff1</a></li>
                              <li><a href="">sub-staff2</a></li>
                          </ul>
                      </li>
                      <li><a href="">contact<a/></li>
                  </ul>
                  

                  数组将动态生成,但限制为 3 个级别,例如:about->about2->sub-sub-about.我尝试解决这个问题:PHP/MySQL Navigation Menu 但它们看起来并不真正得出结论?我熟悉 foreach 的 while 和 for 循环,但我似乎无法理解这个.

                  The array will be dynamically generated, but will have a limit of 3 levels ex: about->about2->sub-sub-about. I tried going off of this question: PHP/MySQL Navigation Menu but they didn't really seem to come to a conclusion? I am familiar with foreach's whiles and for loops but I just can't seem to wrap my head around this one.

                  Enzino,您的代码有效!

                  推荐答案

                  这是我的解决方案:

                  Here is my solution:

                  <?php
                  
                  function MakeMenu($items, $level = 0) {
                      $ret = "";
                      $indent = str_repeat(" ", $level * 2);
                      $ret .= sprintf("%s<ul>
                  ", $indent);
                      $indent = str_repeat(" ", ++$level * 2);
                      foreach ($items as $item => $subitems) {
                          if (!is_numeric($item)) {
                              $ret .= sprintf("%s<li><a href=''>%s</a>", $indent, $item);
                          }
                          if (is_array($subitems)) {
                              $ret .= "
                  ";
                              $ret .= MakeMenu($subitems, $level + 1);
                              $ret .= $indent;
                          } else if (strcmp($item, $subitems)){
                              $ret .= sprintf("%s<li><a href=''>%s</a>", $indent, $subitems);
                          }
                          $ret .= sprintf("</li>
                  ", $indent);
                      }
                      $indent = str_repeat(" ", --$level * 2);
                      $ret .= sprintf("%s</ul>
                  ", $indent);
                      return($ret);
                  }
                  
                  $menu = Array(
                              'home' => Array("sub-home1", "sub-home2"),
                              'about' => Array("sub-about", "about2" => Array("sub-sub-about")),
                              'staff' => Array("sub-staff1", "sub-staff2"),
                              'contact' => "contact"
                          );
                  
                  print_r($menu);
                  
                  echo MakeMenu($menu);
                  
                  ?>
                  

                  这篇关于php从多维数组动态创建导航菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

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

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

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

                            <tfoot id='fkhnp'></tfoot>