我有這樣的陣列由SQL查詢返回的對象,其中top_id是我父ID字段:如何從對象的數組記錄集中獲取嵌套的HTML列表?
Array (
[0] => stdClass Object ([id] => 1 [top_id] => 0 [name] => Cat 1)
[1] => stdClass Object ([id] => 2 [top_id] => 0 [name] => Cat 2)
[2] => stdClass Object ([id] => 3 [top_id] => 0 [name] => Cat 3)
[3] => stdClass Object ([id] => 4 [top_id] => 2 [name] => Subcat 1)
[4] => stdClass Object ([id] => 5 [top_id] => 2 [name] => Subcat 2)
[5] => stdClass Object ([id] => 6 [top_id] => 3 [name] => Subcat 3)
[6] => stdClass Object ([id] => 7 [top_id] => 5 [name] => Subcat 4)
)
現在我需要獲得一個嵌套表像這樣使用PHP:
<ul>
<li>Cat 1</li>
<li>Cat 2
<ul>
<li>Subcat 1</li>
<li>Subcat 2
<ul>
<il>Subcat 3
<ul>
<li>Subcat 4</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>Cat 3</li>
</ul>
任何想法? 由於
的所有映射對象
「top_id」是否指示子類別屬於哪個類別? – TaylorOtwell