3

我正在尝试将扁平的 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. 任何帮助将不胜感激。泰。

4

1 回答 1

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
                        )

                )

        )

)
于 2012-07-28T00:38:30.450 回答