提问人:Shad0w 提问时间:4/3/2023 最后编辑:Shad0w 更新时间:4/6/2023 访问量:83
循环和打印类别 具有多轴子项的树
recrusive looping and printing category Tree with multipule children
问:
我有多个关于我解决问题的方法以及如何解决问题等问题。
问题:我需要一个循环遍历类别树的函数,该类别树最多可以有 6 个下拉列表,并且还返回一个 HTML 字符串,其中包含类别链接的名称及其子类别的
- 递归函数真的是我正在做的事情的最佳方法吗?如果不是,为什么我应该使用什么来代替递归方法?
- 在我的代码中,我试图遍历父级的子级并打印一个 HTML 字符串,该字符串应该看起来像这样,我只有在遍历第一个子项并在遍历第一个子项到第二个子项后才会得到它,例如,每个先前的子项都会被重复:我知道这是因为我调用递归函数的方式,但我迷路了,不知道如何否则就这么称呼它。
ul class="child first"><li class="parent"><a class="arrow-right" href="#">(parent name in the list)</a><ul class='child second'><li class="parent"><a class="arrow-right" href="#">(the name of the first sub child in parent)</a><ul class='child third'><li class="parent"><a href="#">last child of the previous sub child </a></li>
ul class="child first"><li class="parent"><a class="arrow-right" href="#">(parent name in the list)</a><ul class='child second'><li class="parent"><a class="arrow-right" href="#">(the name of the first sub child in parent)</a><ul class='child third'>ul class="child first"><li class="parent"><a class="arrow-right" href="#">(parent name in the list)</a><ul class='child second'><li class="parent"><a class="arrow-right" href="#">(the name of the first sub child in parent)</a><ul class='child third'><li class="parent"><a href="#">last child of the previous sub child </a></li> <ul class='child second'><li class="parent"><a class="arrow-right" href="#">(the name of the second sub child in parent)</a><ul class='child third'>
我的代码:
function loopingThroughTree($listAllEntries, $categoryTree, $i = 0): string
{
$childLists =
[
0 => 'second',
1 => 'third',
2 => 'fourth',
3 => 'fifth',
4 => 'sixth'
];
foreach ($categoryTree['sub'] as $categoryLevel) {
$children [] = $categoryLevel;
if ($categoryLevel['sub'] != null) {
$listAllEntries .= '<li class="parent"><a class="'
. appendArrow($categoryLevel) . '" href="'
. $categoryLevel['link'] . '">' .
$categoryLevel['name'] .
"</a><ul class='child {$childLists[$i]}'>";
$i++;
$children[] = $listAllEntries;
$listAllEntries .= loopingThroughTree($listAllEntries, $categoryLevel, $i);
} else {
$listAllEntries .= '<li class="parent"><a href="'
. $categoryLevel['link'] . '">' .
$categoryLevel['name'] .
'</a></li>';
}
}
$listAllEntries .= "</ul>";
return $listAllEntries;
}
$listAllEntries = '<ul class="child first">';
loopingThroughTree(listAllEntires,categoryTree)
注意:(我知道我的HTML字符串是错误的,那是因为我目前只关注逻辑)
我的列表如下所示示例:
parent => child[0]=>child[0]=>child[0]
child[1]
child[2]=>child[0]
child[1]=>child[0]
child[1]
child[2]=>child[0]
child[1]
child[2]
child[2]
child[1]
child[2]
child[1]=>child[0]
child[1]
child[2]=>child[0]
child[1]
child[2]=>child[0]
child[1]=>child[0]
child[1]
child[2]=>child[0]
child[1]
child[2]
child[2]
答:
0赞
Shad0w
4/4/2023
#1
我仍然不知道我的递归函数是否是最好的方法,但我通过循环遍历子数组并将循环插入字符串中来解决这个问题。$listAllEntries
function loopingThroughTree($listAllEntries, $categoryTree = null, $i = 0): string
{
$children = array();
$childLists =
[
0 => 'second',
1 => 'third',
2 => 'fourth',
3 => 'fifth',
4 => 'sixth'
];
foreach ($categoryTree['sub'] as $categoryLevel => $parent) {
unset($categoryTree[$parent]);
if ($parent['sub'] == null) {
$listAllEntries .= '<li class="parent"><a href="'
. $parent['link'] . '">' .
$parent['name'] .
'</a></li>';
$i = 0;
}else {
$listAllEntries .= '<li class="parent"><a class="'
. appendArrow($parent) . '" href="'
. $parent['link'] . '">' .
$parent['name'] .
"</a><ul class='child {$childLists[$i]}'>";
($categoryLevel != $i) ? $i++ : $i = 0;
$listAllEntries .= loopingThroughTree( $children, $parent, $i);
}
}
$listAllEntries .= "</ul>";
return trim($listAllEntries, "Array");
}
评论