我将使用我的 PDO 数组结果中的 li 和 ul 标签创建一个无限列表(获取)
我的数组:
Array (
[0] => Array ( [category_label] => catLevel4 [category_id] => 22 [category_owner] => 21 )
[1] => Array ( [category_label] => catLevel3 [category_id] => 21 [category_owner] => 20 )
[2] => Array ( [category_label] => catLevel2 [category_id] => 23 [category_owner] => 8 )
[3] => Array ( [category_label] => catLevel2 [category_id] => 24 [category_owner] => 8 )
[4] => Array ( [category_label] => catLevel2 [category_id] => 20 [category_owner] => 6 )
[5] => Array ( [category_label] => catLevel1 [category_id] => 6 [category_owner] => 0 )
[6] => Array ( [category_label] => catLevel1 [category_id] => 7 [category_owner] => 0 )
[7] => Array ( [category_label] => catLevel1 [category_id] => 8 [category_owner] => 0 )
)
我想要 :
catLevel1
catLevel2
catLevel3
catLevel4
catLevel1
catLevel1
catLevel2
catLevel2
我为转换做了一个函数:
function getCategory($parentCategoryId,$data)
{
foreach($data as $row){if($row['category_owner']==$parentCategoryId){?>
<li catId='<?php echo $row['category_id']?>'><a><?php echo $row['category_label'];?></a>
<ul>
<?php getCategory($row['category_id'],$data) ;?>
</ul>
</li>
<?php };}
}
echo getCategory(0,$this->categorysList);
这个功能对我来说非常好用,但是为所有项目创建 null !<ul></ul>
感谢您帮助构建标准功能;D