【发布时间】:2016-09-28 09:30:20
【问题描述】:
我从一个 MYSQL 查询中得到结果,我正在努力把这个数组变成一个嵌套列表:
Array(
[0] => Array
(
[post_title] => Esprit
[post_id] => 240
[folder_id] => 236
)
[1] => Array
(
[post_title] => GC
[post_id] => 241
[folder_id] => 236
)
[2] => Array
(
[post_title] => Guess
[post_id] => 242
[folder_id] => 236
[children] => Array
(
[0] => Array
(
[post_title] => Jewels
[post_id] => 250
[folder_id] => 242
[children] => Array
(
[0] => Array
(
[post_title] => Products-shots
[post_id] => 251
[folder_id] => 250
[children] => Array
(
[0] => Array
(
[post_title] => New-Collection
[post_id] => 252
[folder_id] => 251
)
)
)
)
)
)
)
[3] => Array
(
[post_title] => Guess-Connect
[post_id] => 243
[folder_id] => 236
)
[4] => Array
(
[post_title] => Nautica
[post_id] => 244
[folder_id] => 236
)
[5] => Array
(
[post_title] => Obaku
[post_id] => 245
[folder_id] => 236
)
[6] => Array
(
[post_title] => Police
[post_id] => 246
[folder_id] => 236
)
[7] => Array
(
[post_title] => Roamer
[post_id] => 247
[folder_id] => 236
)
[8] => Array
(
[post_title] => Superdry
[post_id] => 248
[folder_id] => 236
)
[9] => Array
(
[post_title] => Vulcain
[post_id] => 249
[folder_id] => 236
)
)
我尝试了很多代码,但没有一个能得到这么精确的输出:
<ul>
<li>Esprit</li>
<li>GC</li>
<li>Guess
<ul>
<li>Jewels</li>
<li>Products-shot</li>
<li>New-collection</li>
</ul>
</li>
<li>Guess-Connect</li>
<li>Nautica</li>
<li>Obaku</li>
<li>Police</li>
<li>Roamer</li>
<li>Superdry</li>
<li>Vulcain</li>
</ul>
非常感谢帮助!提前致谢
【问题讨论】:
-
SQL 结果结构错误。您有嵌套的孩子,但您想将它们视为同一父母的孩子。怎么了?你的概念,还是数据库结构?
-
实际上我从 phpMyAdmin 获得了未嵌套的数据。我使用一个函数来构建一个新数组,根据“parent_id”列将孩子嵌套到他们的父母。
标签: php arrays list multidimensional-array nested