• <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从多维数组动态创建导航菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  PHP Upload File Validation(PHP 上传文件验证)
                  PHP Error - Uploading a file(PHP 错误 - 上传文件)
                  How can I write tests for file upload in PHP?(如何在 PHP 中编写文件上传测试?)
                  php resizing image on upload rotates the image when i don#39;t want it to(php在上传时调整图像大小会在我不想要它时旋转图像)
                  How to send additional data using PLupload?(如何使用 PLupload 发送附加数据?)
                  change button text in js/ajax after mp4 =gt;mp3 conversion in php(在 php 中的 mp4 =gt;mp3 转换后更改 js/ajax 中的按钮文本)

                  • <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>