将修改后的预订树遍历数据转换为数组

时间:2009-09-24 18:41:38

标签: php mysql

将树数据放入数组的有效方法是什么?

我按照sitepoint tutorial检索树数据。

但是,本教程仅介绍如何输出树,而不是如何制作多维数组。

我用过

SELECT title, lft, rgt FROM tree_structure WHERE lft BETWEEN $parentLft  AND $parentRgt ORDER BY lft ASC

因此,对于每个项目,我都有其标题,左右值。

我坚持让阵列看起来像这样

Array
(
 Title: Main Topic
 Children => Array
             (
              => Title: subTopic
                     Leaf:  true
              => Title: Another subtopic
                     Children =>  Array
                               (
                                => Title: subtopic child
                                  Leaf: true
                               )
              ) 

)

如果你能提供帮助,我会非常感激。

PS。 sql输出看起来like this(除了我有标题,不是名字,不使用category_id):

+-------------+----------------------+-----+-----+
| category_id | name                 | lft | rgt |
+-------------+----------------------+-----+-----+
|           1 | ELECTRONICS          |   1 |  20 |
|           2 | TELEVISIONS          |   2 |   9 |
|           3 | TUBE                 |   3 |   4 |
|           4 | LCD                  |   5 |   6 |
|           5 | PLASMA               |   7 |   8 |
|           6 | PORTABLE ELECTRONICS |  10 |  19 |
|           7 | MP3 PLAYERS          |  11 |  14 |
|           8 | FLASH                |  12 |  13 |
|           9 | CD PLAYERS           |  15 |  16 |
|          10 | 2 WAY RADIOS         |  17 |  18 |

3 个答案:

答案 0 :(得分:6)

快速提供此代码。 $ results是数据库结果。 $ tree是你要回来的数组。

function create_tree ($results) {

    $return = $results[0];
    array_shift($results);

    if ($return['lft'] + 1 == $return['rgt'])
        $return['leaf'] = true;
    else {
        foreach ($results as $key => $result) {
            if ($result['lft'] > $return['rgt']) //not a child
                break;
            if ($rgt > $result['lft']) //not a top-level child
                continue;
            $return['children'][] = create_tree(array_values($results));
            foreach ($results as $child_key => $child) {
                if ($child['rgt'] < $result['rgt'])
                    unset($results[$child_key]);
            }
            $rgt = $result['rgt'];
            unset($results[$key]);
        }
    }

    unset($return['lft'],$return['rgt']);
    return $return;

}
$tree = create_tree($results);

答案 1 :(得分:4)

我会从重写SQL查询开始:

SELECT title, (SELECT TOP 1 title 
               FROM tree t2 
               WHERE t2.lft < t1.lft AND t2.rgt > t1.rgt    
               ORDER BY t2.rgt-t1.rgt ASC) AS parent
FROM tree t1
ORDER BY rgt-lft DESC

这会给你这样的结果:

title                | parent
----------------------------------------------
ELECTRONICS          | NULL
PORTABLE ELECTRONICS | ELECTRONICS
TELEVISIONS          | ELECTRONICS
MP3 PLAYERS          | PORTABLE ELECTRONICS
FLASH                | MP3 PLAYERS
CD PLAYERS           | PORTABLE ELECTRONICS
2 WAY RADIOS         | PORTABLE ELECTRONICS
TUBE                 | TELEVISIONS
LCD                  | TELEVISIONS
PLASMA               | TELEVISIONS

有了这个,就容易多了。

答案 2 :(得分:0)

使用create_tree我此时收到错误:

if ($rgt > $result['lft']) //not a top-level child  continue;

它返回的错误是:未定义的变量:rgt

它也没有返回正确的数组..... 我在

中使用相同的数据库结构

http://articles.sitepoint.com/article/hierarchical-data-database/2