php 递归循环和打印范畴多子树

4si2a6ki  于 2023-04-04  发布在  PHP
关注(0)|答案(1)|浏览(121)

我有很多关于我解决问题的方法和如何解决它的问题。

问题:我需要一个函数,通过一个类别树循环,其中可能有多达6个唐斯,并返回一个HTML字符串,其中包含类别链接的名称及其子类别的

1.递归函数真的是我正在做的事情的最佳方法吗?如果不是,为什么?我应该使用什么来代替递归方法?
1.在我的代码中,我试图循环遍历父节点的子节点,并打印一个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]
2wnc66cl

2wnc66cl1#

我仍然不知道我的递归函数是否是最好的方法,但我通过循环子数组并将循环插入到$listAllEntries字符串中来解决这个问题。

function loopingThroughTree($listAllEntries, $categoryTree = null, $i = 0): string
{

    $children = array();
    $htmlChildren = 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");

}

相关问题