递归树遍历 - 如何跟踪递归级别? [英] recursive tree traversal - How to keep track of the recursion level?

查看:75
本文介绍了递归树遍历 - 如何跟踪递归级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上是在尝试从表示树结构的多维数组构建一个 html ul/li 嵌套列表.

I'm basically trying to build an html ul/li nested list from a multidimensional array representing a tree structure.

以下代码工作正常,但我想改进它:

The following code works fine but I want to improve it:

我需要一种方法来跟踪递归级别,以便我可以将不同的类应用于不同的级别,向生成的输出添加缩进等.

I need a way to keep track of the recursion level so I can apply different classes to different levels, add indenting to the generated output, etc.

function buildTree($tree_array, $display_field, $children_field, $class='', $id='') {

  echo "<ul>\n";

  foreach ($tree_array as $row) {

    echo "<li>\n";
    echo $row[$display_field] . "\n";

    if (isset($row[$children_field])) {

      $this->buildTree($row[$children_field]);
    }
    echo "</li>\n";
  }
  echo "</ul>\n";
} 

$tree_array 看起来像这样:

The $tree_array looks like this:

Array
(
    [0] => Array
        (
            [category_id] => 1
            [category_name] => calculatoare
            [parent_id] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [category_id] => 4
                            [category_name] => placi de baza
                            [parent_id] => 1
                        )

                    [1] => Array
                        (
                            [category_id] => 5
                            [category_name] => carcase
                            [parent_id] => 1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [category_id] => 6
                                            [category_name] => midi-tower
                                            [parent_id] => 5
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [category_id] => 2
            [category_name] => electronice
            [parent_id] => 0
        )

    [2] => Array
        (
            [category_id] => 3
            [category_name] => carti
            [parent_id] => 0
        )

)

我已将其标记为作业,因为我想以此为契机来提高我对递归的(差的)理解,因此,我希望能够指导我找到解决方案而不是提供完整的答案工作示例:)

I've tagged this as homework because I'd like to use this as an opportunity to improve on my (poor) understanding of recursion so, I'd appreciate answers that would guide me to the solution rather than provide a complete working example :)

推荐答案

Quick'n'dirty 方法(参见下面的剧透"块以了解实现):

Quick'n'dirty approach (see "spoiler" block below for the implementation):

在你的函数声明中添加一个额外的变量 $recursionDepth,默认为 0.

Add an additional variable $recursionDepth to your function declaration, make it 0 by default.

在每个后续递归中,使用 $recursionDepth + 1 调用您的函数.

On each subsequent recursion, call your function with $recursionDepth + 1.

由于函数变量仅对函数的各个实例可见"(作用域),因此您最终会得到当前迭代深度的指示器.

Since the function variables are only "visible" (scoped) for the respective instance of the function, you'll end up with an indicator of the current iteration depth.

此外,函数的第 12 行

Also, line 12 of your function

$this->buildTree();

在我看来它不会起作用——原因是您没有将变量传递给 buildTree 的下一个实例.

doesn't look to me as if it would work – the reason being that you're not passing your variables on to the next instance of buildTree.

它可能应该是这样的:

$this->buildTree($row[$children_field], $display_field, $children_field, $class, $id)

以下是我对您的代码进行的更改以实现您想要的:

Here's the changes I'd make to your code to achieve what you want:

function buildTree($tree_array, $display_field, $children_field, $class='', $id='', $recursionDepth = 0, $maxDepth = false)
{
    if ($maxDepth && ($recursionDepth == $maxDepth)) return;

    echo "<ul>\n";

    foreach ($tree_array as $row)
    {
        echo "<li>\n";
        echo $row[$display_field] . "\n";

        if (isset($row[$children_field]))
            $this->buildTree($row[$children_field], $display_field, $children_field, $class, $id, $recursionDepth + 1, $maxDepth);

        echo "</li>\n";
    }
    echo "</ul>\n";
}

这篇关于递归树遍历 - 如何跟踪递归级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆