doqpm82240 2014-04-27 09:52
浏览 67

通过多维PHP数组进行递归以生成HTML列表

Sadly I struggle with anything where recursion is involved and this problem is no different.

I have a category structure in a PHP array. I have grouped it by parent categories like this, but if a solution warrants it, I would be happy to change that:

array(5) {
  [0] => array(4) {
    [50] => array(5) {
      ["category_id"] => int(50)
      ["category_title"] => string(29) "Category 50"
      ["parent_category_id"] => int(0)
      ["display_order"] => int(0)
      ["depth"] => int(0)
    }
    [1] => array(5) {
      ["category_id"] => int(1)
      ["category_title"] => string(24) "Category 1"
      ["parent_category_id"] => int(0)
      ["display_order"] => int(1)
      ["depth"] => int(0)
    }
    [80] => array(5) {
      ["category_id"] => int(80)
      ["category_title"] => string(27) "Category 80"
      ["parent_category_id"] => int(0)
      ["display_order"] => int(1)
      ["depth"] => int(0)
    }
    [10] => array(5) {
      ["category_id"] => int(10)
      ["category_title"] => string(29) "Category 10"
      ["parent_category_id"] => int(0)
      ["display_order"] => int(10)
      ["depth"] => int(0)
    }
  }
  [1] => array(1) {
    [2] => array(5) {
      ["category_id"] => int(2)
      ["category_title"] => string(21) "Category 2"
      ["parent_category_id"] => int(1)
      ["display_order"] => int(1)
      ["depth"] => int(1)
    }
  }
  [30] => array(1) {
    [90] => array(5) {
      ["category_id"] => int(90)
      ["category_title"] => string(11) "Category 90"
      ["parent_category_id"] => int(30)
      ["display_order"] => int(1)
      ["depth"] => int(2)
    }
  }
  [10] => array(2) {
    [20] => array(5) {
      ["category_id"] => int(20)
      ["category_title"] => string(21) "Category 20"
      ["parent_category_id"] => int(10)
      ["display_order"] => int(10)
      ["depth"] => int(1)
    }
    [30] => array(5) {
      ["category_id"] => int(30)
      ["category_title"] => string(17) "Category 30"
      ["parent_category_id"] => int(10)
      ["display_order"] => int(20)
      ["depth"] => int(1)
    }
  }
  [50] => array(3) {
    [40] => array(5) {
      ["category_id"] => int(40)
      ["category_title"] => string(6) "Category 40"
      ["parent_category_id"] => int(50)
      ["display_order"] => int(1000)
      ["depth"] => int(1)
    }
    [60] => array(5) {
      ["category_id"] => int(60)
      ["category_title"] => string(6) "Category 60"
      ["parent_category_id"] => int(50)
      ["display_order"] => int(2000)
      ["depth"] => int(1)
    }
    [70] => array(5) {
      ["category_id"] => int(70)
      ["category_title"] => string(17) "Category 70"
      ["parent_category_id"] => int(50)
      ["display_order"] => int(3000)
      ["depth"] => int(1)
    }
}

So the end result of the HTML ought to look like this:

<ul>
    <li>Category 50</li>
    <ul>
        <li>Category 40</li>
        <li>Category 60</li>
        <li>Category 70</li>
    </ul>
    <li>Category 1</li>
    <ul>
        <li>Category 2</li>
    </ul>
    <li>Category 80</li>
    <li>Category 10</li>
    <ul>
        <li>Category 20</li>
        <li>Category 30</li>
        <ul>
            <li>Category 90</li>
        </ul>
    </ul>
</ul>

Note that there could be multiple levels of nesting.

Although not depicted in the above example, it would be useful to be able to use more than just 'category_title' for each list item (there are some other parameters not shown, e.g. counts and other stats that have been removed for simplicity).

I am incredibly appreciative that you have even read this far. Any suggestions gratefully received.

Visual representation of the end result HTML:

I am looking to convert this into a HTML list like this:

  • Category 50
    • Category 40
    • Category 60
    • Category 70
  • Category 1
    • Category 2
  • Category 80
  • Category 10
    • Category 20
    • Category 30
      • Category 90
  • 写回答

1条回答 默认 最新

  • duanlieshuang5330 2014-04-27 10:02
    关注

    This should work. Note that this is an untested script:

    function recursiveList($array) {
       echo '<ul>';
       foreach ($array as $key => $value) {
           if (is_array($value) && !isset($value['category_id'])) { // if isset fails means that there is an array containing other categories arrays
               recursiveList($value);
           }
           echo '<li>' . $value['category_title'] . '</li>';
       }
       echo '</ul>';
    }
    
    recursiveList($categories);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程