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',
            ),  
        );  

最初にすべての第1レベルのノードを初期化し、次に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);

        }   
    }   

}   

これは、ツリーの第1レベルと第2レベルのみを返します。

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の子が1つあることを確認し、それを'children'に正しく追加します)

それはそれを保存していないだけです!誰かが私が間違っていることを見ることができますか?私がvar_dump階層化すると、3番目がで正常に作成されていても、それは1番目と2番目のレベルであり、3番目ではありませんbuild_tree。どんな助けでも大歓迎です。Ty。

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 に答える