将我的数据库表转换为树,并在php中获取叶节点 [英] convert my database table to a tree and get leaf nodes in php
问题描述
在这个表格中,我有 PreferenceID
和 PreferenceParentID
。
在这种情况下,我想建一棵树。
级别 1
应该是
时尚
和音乐
,因为他们有 PreferenceParentID = 0
在 2
nd级别男士服装
应该在时尚
下,因为它的父首选项ID是时尚。和艺术家
在音乐
之下。
在 3
级别 couture
和 denims
应该在男士服装
和非洲
和
在艺术家
下
,我想获得所有叶节点值。在这种情况下,我想要获得
couture
和 denims
和非洲和
afrobeat`。
树可能长达n个级别。
请帮助我。任何建议是欢迎........................ D
对于Chauhan的链接文章,我想发布一个更简单的解决方案:
//示例数据(从一个大的查询中选择一个大的查询)
$ rows = array(
array('id'=> 971,'parent_id'=> 3,'label'=>' '),
数组('id'=> 972,'parent_id'=> 3,'标签'=>'电影明星'),
数组('id'=> 1 ,'parent_id'=> 0,'label'=>'Fashion'),
array('id'=> 32,'parent_id'=> 1,'label'=& \'的服装'),
数组('id'=> 45,'parent_id'=> 32,'label'=& => 55,'parent_id'=> 32,'label'=>'Denims'),
array('id'=> 2,'parent_id'=> 0,'label'= >'音乐'),
数组('id => 970,'parent_id'=> 2,'label'=>'Artists'),
array('id'=> 1118,'parent_id'=> 970,'label'= >'African'),
array('id'=> 1119,'parent_id'=> 970,'label'=> 'Afrobeat'),
);
//构建地图并收集ids
$ map = array();
$ ids = array();
foreach($ rows as $ row){//可以使用典型的mysql_fetch_ *东西
if(!isset($ map [$ row ['parent_id']])){
$ map [$ row ['parent_id']] = array();
}
$ map [$ row ['parent_id']] [] = $ row;
$ ids [] = $ row ['id'];
}
//递归助手显示
函数助手($ map,$ parentId = 0){
echo'< ul>';
foreach($ map [$ parentId] as $ entry){
printf('< li> [%s]%s',$ entry ['id'],$ entry ['label' ]);
if(isset($ map [$ entry ['id']])){
helper($ map,$ entry ['id']);
}
echo'< / li>';
}
echo'< / ul>';
}
// create ul
helper($ map);
//叶节点
print_r(
array_diff($ ids,array_keys($ map))
);
我也喜欢说,如果这样的数据库结构无法避免,递归查询可能是最坏的事情要做,表现明智。
hi i have a database table , i want to set that as a tree structure and get the leaf nodes of that tree .
in this table i have PreferenceID
and PreferenceParentID
.
in this case i want to built a tree .
level 1
should be fashion
and music
, because they have the PreferenceParentID = 0
in 2
nd level men's clothing
should be under fashion
because it's parent preference id is fashion . and Artists
sholud be under music
.
in 3
level couture
and denims
should be under men's clothing
and african
and afrobeat
shoul be under Artists
.
and i want to get all the leaf node values . in this case i want to get
couture
and denims
and africanand
afrobeat`.
tree may grow up to n levels .
please help me . any suggestion is welcome ....................... :D
In response to Chauhan's linked article, I'd like to post a much simpler solution:
// sample data (from one big query selecting them in one go)
$rows = array(
array('id' => 971, 'parent_id' => 3, 'label' => 'Genres'),
array('id' => 972, 'parent_id' => 3, 'label' => 'Movie Stars'),
array('id' => 1, 'parent_id' => 0, 'label' => 'Fashion'),
array('id' => 32, 'parent_id' => 1, 'label' => 'Men\'s Clothing'),
array('id' => 45, 'parent_id' => 32, 'label' => 'Couture'),
array('id' => 55, 'parent_id' => 32, 'label' => 'Denims'),
array('id' => 2, 'parent_id' => 0, 'label' => 'Music'),
array('id' => 970, 'parent_id' => 2, 'label' => 'Artists'),
array('id' => 1118, 'parent_id' => 970, 'label' => 'African'),
array('id' => 1119, 'parent_id' => 970, 'label' => 'Afrobeat'),
);
// build map and collect ids
$map = array();
$ids = array();
foreach ($rows as $row) { // one could use the typical mysql_fetch_* stuff here
if (!isset($map[$row['parent_id']])) {
$map[$row['parent_id']] = array();
}
$map[$row['parent_id']][] = $row;
$ids[] = $row['id'];
}
// recursive helper display
function helper($map, $parentId = 0) {
echo '<ul>';
foreach ($map[$parentId] as $entry) {
printf('<li>[%s] %s', $entry['id'], $entry['label']);
if (isset($map[$entry['id']])) {
helper($map, $entry['id']);
}
echo '</li>';
}
echo '</ul>';
}
// create ul
helper($map);
// the leaf nodes
print_r(
array_diff($ids, array_keys($map))
);
I also like to say, that, if such database structures cannot be avoided, recursive queries is probably the worst thing to do, performance wise.
这篇关于将我的数据库表转换为树,并在php中获取叶节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!