【发布时间】:2021-09-18 20:27:51
【问题描述】:
有一个数组,数组的每个元素内部都有一个groupId,数组的一些元素有子元素。 如何获得这种输出:
<div class="parentCategory">
<p>Japanese kitchen</p>
<div class="childCategory">
<a class="hasChilds" href="#">Sushi</a>
<div class="childrenCategory">
<p>Hot Sushi</p>
<p>Vegan Sushi</p>
</div>
<a href="#">Rolls</a>
<a href="#">Onigiri</a>
</div>
</div>
数组中的父类:
[75] => Array
(
[groupName] => Japanese kitchen
[groupId] => 100001
[hasChilds] => 1
)
数组中的子类:
[76] => Array
(
[groupName] => Sushi
[groupId] => 101705
[hasChilds] => 1
[parentGroupId] => 100001
)
我只能返回父类别,如何链接子类别,反过来我不知道如何
<? foreach($getGroup['Groups'] as $echoGroup) {
if($echoGroup['hasChilds'] != null) {
echo '<li class="parent"><a href="#">' . $echoGroup['groupName'] . '</li>';
}
} ?>
【问题讨论】:
-
...如何获得这种输出...请发布您试图实现该目标的代码并描述什么不起作用。
-
不幸的是,我只能显示没有子类别的父类别,更新了问题
-
看似迂腐,但关于英语复数的重要说明:“child”的复数形式是“children”,因此您可能希望将
hasChilds重命名为hasChildren,这使其符合用英语编写的所有其他与儿童相关的代码。