【问题标题】:Recursion Help changes aren't being saved after the first and second level在第一级和第二级之后没有保存递归帮助更改
【发布时间】:2012-07-27 14:27:07
【问题描述】:

我正在尝试将扁平的 mysql 行转换为树结构。

$categories = array(
            array(
                'id' => '1',
                'name' => 'root',
                'parent' => '0',
            ),  
            array(
                'id' => '2',
                'name' => 'first',
                'parent' => '1',
            ),  
            array(
                'id' => '3',
                'name' => 'first',
                'parent' => '1',
            ),  
            array(
                'id' => '4',
                'name' => 'second',
                'parent' => '3',
            ),  
        );  

我先初始化所有一级节点,然后在每个节点上调用build_tree

    $hierarchy = array();
    // loop through and get each root node
    foreach($categories as $key => $category) {
        if ($category['parent'] == 0) {

            // initialize this root
            $hierarchy[$category['id']] = $category;
            $hierarchy[$category['id']]['children'] = array();

            // remove this from categories
            unset($categories[$key]);

            $this->build_tree($hierarchy[$category['id']], $categories);

        }   
    }   

    return $hierarchy;

}   



function build_tree(&$node, &$categories) {

    foreach ($categories as $key => $category) {

        // check if this node is the parent
        if ($node['id']  === $category['parent']) {

            $node['children'][$category['id']] = $category;
            $node['children'][$category['id']]['children'] = array();
            unset($categories[$key]);

            $this->build_tree($category, $categories);

        }   
    }   

}   

这只是返回树的第一层和第二层。

array
  1 => 
    array
      'id' => string '1' (length=1)
      'name' => string 'root' (length=4)
      'parent' => string '0' (length=1)
      'children' => 
        array
          2 => 
            array
              'id' => string '2' (length=1)
              'name' => string 'first' (length=5)
              'parent' => string '1' (length=1)
              'children' => 
                array
                  empty
          3 => 
            array
              'id' => string '3' (length=1)
              'name' => string 'first' (length=5)
              'parent' => string '1' (length=1)
              'children' => 
                array
                  empty

build_tree 内部,当它到达id=2 时,它正在成功创建子代。 (发现有一个 id=2 的孩子并将其正确附加到 'children' )

只是没有保存它!谁能看到我做错了什么?当我var_dump 层次结构时,即使在build_tree 中成功创建了第三层,它也只是第一层和第二层,而不是第三层。任何帮助将不胜感激。泰。

【问题讨论】:

  • 我发现在编写递归函数时,向后工作更有效——先编写转义案例,然后编写延续案例——这就是你开发此函数的方式吗?
  • 我猜你需要在这里循环引用:foreach ($categories as $key => &$category) {。注意添加了&。这样能解决吗?
  • @nickb 不,没有解决它,马特没有机智地开发它,只是摸索了几个小时,当我终于得到第一和第二级时,我只是试图用它滚动.我会先尝试编写转义案例,ty
  • Sample code 应该是完整的。给定的代码缺少第一个方法中的一些行以及示例数据(作为 PHP 数组)和启动它的方法调用。

标签: php recursion tree


【解决方案1】:

为了提高性能,在递归解决方案之前考虑非递归解决方案通常是一个好习惯。我整理了一些代码,希望可以帮助您解决类别树问题:

$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$sth = $dbh->prepare('SELECT * FROM categories');
$sth->execute();
$categories = $sth->fetchAll();

function buildCategoryTree(&$categories)
{

    $tree = array(0=>array('children'=>array()));

    foreach($categories as &$c)
    {
        $tree[$c['id']] = &$c;
    }

    foreach($categories as &$c)
    {
        $parent_id = $c['parent'] ?: 0;
        if(!isset($tree[$parent_id])) $tree[$parent_id]['children'] = array();
        $tree[$parent_id]['children'][] = &$c;
    }

    return $tree[0]['children'];
}

$tree = buildCategoryTree($categories);

如果您在数据库中有以下内容:

mysql> SELECT * FROM categories;
+----+------+--------+
| id | name | parent |
+----+------+--------+
|  1 | A    |      0 |
|  2 | B    |      0 |
|  3 | C    |      0 |
|  4 | A1   |      1 |
|  5 | A2   |      1 |
|  6 | B1   |      2 |
|  7 | B2   |      2 |
|  8 | C1   |      3 |
|  9 | A3   |      1 |
| 10 | B2i  |      7 |
| 11 | A1ii |      4 |
| 12 | A1i  |      4 |
+----+------+--------+

那么代码将产生$tree

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => A
            [parent] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [name] => A1
                            [parent] => 1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 11
                                            [name] => A1ii
                                            [parent] => 4
                                        )

                                    [1] => Array
                                        (
                                            [id] => 12
                                            [name] => A1i
                                            [parent] => 4
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 5
                            [name] => A2
                            [parent] => 1
                        )

                    [2] => Array
                        (
                            [id] => 9
                            [name] => A3
                            [parent] => 1
                        )

                )

        )

    [1] => Array
        (
            [id] => 2
            [name] => B
            [parent] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 6
                            [name] => B1
                            [parent] => 2
                        )

                    [1] => Array
                        (
                            [id] => 7
                            [name] => B2
                            [parent] => 2
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 10
                                            [name] => B2i
                                            [parent] => 7
                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [id] => 3
            [name] => C
            [parent] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 8
                            [name] => C1
                            [parent] => 3
                        )

                )

        )

)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 2015-03-22
    • 2014-12-13
    • 2014-02-18
    • 2021-10-20
    相关资源
    最近更新 更多