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

时间:2022-06-12 21:31:26

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:

我在PHP数组中有一个类别结构。我已按照这样的父类别对其进行分组,但如果解决方案需要保证,我很乐意改变它:

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:

所以HTML的最终结果应该是这样的:

<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).

尽管在上面的示例中没有描述,但是对于每个列表项能够使用不仅仅是“category_title”将是有用的(还有一些其他参数未示出,例如为了简单而已经移除的计数和其他统计)。

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

我非常感谢你甚至读过这篇文章。感激地收到任何建议。

Visual representation of the end result HTML:

最终结果HTML的可视化表示:

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

我希望将其转换为HTML列表,如下所示:

  • Category 50
  • 第50类
    • Category 40
    • 40类
    • Category 60
    • 60类
    • Category 70
    • 第70类
  • Category 1
  • 第1类
    • Category 2
    • 第2类
  • Category 80
  • 80类
  • Category 10
  • 第10类
    • Category 20
    • 第20类
    • Category 30
    • 第30类
      • Category 90
      • 第90类

1 个解决方案

#1


1  

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);

#1


1  

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);