PHP从平面数据集(从Mysql表)创建多维对象
以下是mysql表中的数据集: 使用MySQL嵌套集模型并不明显,因为我省略了PHP从平面数据集(从Mysql表)创建多维对象,php,arrays,zend-framework,object,Php,Arrays,Zend Framework,Object,以下是mysql表中的数据集: 使用MySQL嵌套集模型并不明显,因为我省略了lft和rgt列 +------+----------+--------------------------------+-------+ + Id | ParentId | Name | Level | +------+----------+--------------------------------+-------+ | 1001 | NULL |
lft
和rgt
列
+------+----------+--------------------------------+-------+
+ Id | ParentId | Name | Level |
+------+----------+--------------------------------+-------+
| 1001 | NULL | Computing | 0 |
| 1002 | NULL | Cassettes & Vinyl | 0 |
| 1003 | 1001 | CD Players | 1 |
| 1004 | 1002 | CD Writers | 1 |
| 1005 | 1003 | CD-ROM Drives | 2 |
| 1006 | 1004 | CDs | 2 |
+------+----------+--------------------------------+-------+
将其拉入二维数组:
<?php
$data = array(
array('id' => 1001, 'ParentId' => NULL, 'Name' => 'Computing', 'Level' => 0),
array('id' => 1002, 'ParentId' => NULL, 'Name' => 'Cassettes & Vinyl', 'Level' => 0),
array('id' => 1003, 'ParentId' => 1001, 'Name' => 'CD Players', 'Level' => 1),
array('id' => 1004, 'ParentId' => 1002, 'Name' => 'CD Writers', 'Level' => 1),
array('id' => 1005, 'ParentId' => 1003, 'Name' => 'CD-ROM Drives', 'Level' => 2),
array('id' => 1006, 'ParentId' => 1004, 'Name' => 'Computing', 'Level' => 3)
);
?>
我一直在玩递归函数,运气不好
为什么此对象将用于创建Zend_导航。我不想在这个阶段使用XML文件,我更喜欢在数据库中进行分类管理
有什么想法吗?这不一定是递归的工作,可以通过迭代轻松完成
<?php
$data = array(
array('id' => 1001, 'ParentId' => NULL, 'Name' => 'Computing', 'Level' => 0),
array('id' => 1002, 'ParentId' => NULL, 'Name' => 'Cassettes & Vinyl', 'Level' => 0),
array('id' => 1003, 'ParentId' => 1001, 'Name' => 'CD Players', 'Level' => 1),
array('id' => 1004, 'ParentId' => 1002, 'Name' => 'CD Writers', 'Level' => 1),
array('id' => 1005, 'ParentId' => 1003, 'Name' => 'CD-ROM Drives', 'Level' => 2),
array('id' => 1006, 'ParentId' => 1004, 'Name' => 'Computing', 'Level' => 3)
);
$index = array();
$tree = array();
// step 1: build index (note that I use &$row references!)
foreach ($data as &$row) {
$index[$row['id']] = &$row;
$row['children'] = array();
if ($row['ParentId'] == NULL) {
$tree[] = &$row;
}
}
// step 2: link tree (references here as well!)
foreach ($data as &$row) {
$index[$row['ParentId']]['children'][] = &$row;
}
print_r($tree);
?>
Id使用mysql\u fetch\u对象从数据库中提取数据,代码会变成什么?使用
mysql\u fetch\u assoc()
怎么样?那就不那么麻烦了。除此之外,上面的代码非常简单。对你来说,做出必要的改变应该不难。
<?php
$data = array(
array('id' => 1001, 'ParentId' => NULL, 'Name' => 'Computing', 'Level' => 0),
array('id' => 1002, 'ParentId' => NULL, 'Name' => 'Cassettes & Vinyl', 'Level' => 0),
array('id' => 1003, 'ParentId' => 1001, 'Name' => 'CD Players', 'Level' => 1),
array('id' => 1004, 'ParentId' => 1002, 'Name' => 'CD Writers', 'Level' => 1),
array('id' => 1005, 'ParentId' => 1003, 'Name' => 'CD-ROM Drives', 'Level' => 2),
array('id' => 1006, 'ParentId' => 1004, 'Name' => 'Computing', 'Level' => 3)
);
$index = array();
$tree = array();
// step 1: build index (note that I use &$row references!)
foreach ($data as &$row) {
$index[$row['id']] = &$row;
$row['children'] = array();
if ($row['ParentId'] == NULL) {
$tree[] = &$row;
}
}
// step 2: link tree (references here as well!)
foreach ($data as &$row) {
$index[$row['ParentId']]['children'][] = &$row;
}
print_r($tree);
?>
Array
(
[0] => Array
(
[id] => 1001
[ParentId] =>
[Name] => Computing
[Level] => 0
[children] => Array
(
[0] => Array
(
[id] => 1003
[ParentId] => 1001
[Name] => CD Players
[Level] => 1
[children] => Array
(
[0] => Array
(
[id] => 1005
[ParentId] => 1003
[Name] => CD-ROM Drives
[Level] => 2
[children] => Array
(
)
)
)
)
)
)
[1] => Array
(
[id] => 1002
[ParentId] =>
[Name] => Cassettes & Vinyl
[Level] => 0
[children] => Array
(
[0] => Array
(
[id] => 1004
[ParentId] => 1002
[Name] => CD Writers
[Level] => 1
[children] => Array
(
[0] => Array
(
[id] => 1006
[ParentId] => 1004
[Name] => Computing
[Level] => 3
[children] => Array
(
)
)
)
)
)
)
)