一般在读取用“邻接列表算法”组织的数据时,需要使用递归逐层读取
或者读取数据到数组,然后用递归或非递归的方法再行处理
这里介绍一种边读边生成“树状”数组的方法,希望对你有用
mysql_connect();//测试数据$sql =< 1
[pid] => 0
[title] => food
[child] => array
(
[2] => array
(
[id] => 2
[pid] => 1
[title] => fruit
[child] => array
(
[3] => array
(
[id] => 3
[pid] => 2
[title] => red
[child] => array
(
[4] => array
(
[id] => 4
[pid] => 3
[title] => cherry
)
)
)
[5] => array
(
[id] => 5
[pid] => 2
[title] => yellow
[child] => array
(
[6] => array
(
[id] => 6
[pid] => 5
[title] => banana
)
)
)
)
)
[7] => array
(
[id] => 7
[pid] => 1
[title] => meat
[child] => array
(
[8] => array
(
[id] => 8
[pid] => 7
[title] => beef
)
[9] => array
(
[id] => 9
[pid] => 7
[title] => pork
)
)
)
)
)
)
回复讨论(解决方案) 支持版主
工作几年,一直用这用处理无限分类,这个类不是我写的,希望作者来认领,有时也要扩展一些方法。但基础方法已经足够了
class tree{ public $data=array(); public $catearray=array(); function tree() { } function setnode ($id, $parent, $value) { $parent = $parent?$parent:0; $this->data[$id] = $value; $this->catearray[$id] = $parent; } function getchildstree($id=0) { $childs=array(); foreach ($this->catearray as $child=>$parent) { if ($parent==$id) { $childs[$child]=$this->getchildstree($child); } } return $childs; } function getparentstree($id=0) { $parents=array(); foreach ($this->catearray as $child=>$parent) { if ($child ==$id) { $parents[$parent]=$this->getparentstree($parent); } } return $parents; } function getchilds($id=0) { $childarray=array(); $childs=$this->getchild($id); foreach ($childs as $child) { $childarray[]=$child; $childarray=array_merge($childarray,$this->getchilds($child)); } return $childarray; } function getchild($id) { $childs=array(); foreach ($this->catearray as $child=>$parent) { if ($parent==$id) { $childs[$child]=$child; } } return $childs; } function getparents($id) { $parentarray=array(); $parents=$this->getparent($id); foreach ($parents as $parent) { $parentarray[]=$parent; $parentarray=array_merge($parentarray,$this->getparents($parent)); } return $parentarray; } function getparent($id) { $parents=array(); foreach ($this->catearray as $child=>$parent) { if ($child==$id) { $parents[$parent]=$parent; } } return $parents; } //单线获取父节点 function getnodelever($id) { $parents=array(); if (key_exists($this->catearray[$id],$this->catearray)) { $parents[]=$this->catearray[$id]; $parents=array_merge($parents,$this->getnodelever($this->catearray[$id])); } return $parents; } function getlayer($id,$prestr='|-') { return str_repeat($prestr,count($this->getnodelever($id))); } function getvalue ($id) { return $this->data[$id]; } // end func}
=& 这个是运算符?
很好的算法,参考一下
不错,值得借鉴。
感谢版主分享
支持版主
=& 这个是运算符?
方法不错,收藏
初学者,看的有点蒙
好东西!!
研究盐?鸡,好东西
很好的方法
正在找这捏~谢楼主~
我以为语言都是相通的,但是还是看不懂,看样功力不够
还好 谢谢贴主赐教 。。。
版主大哥~~再把你这个输出的树状数组转成以id排序的二维数组呢?
array([1]=>([id] => 1
[pid] => 0
[title] => food),
[2]=>]=>([id] => 2
[pid] => 0
[title] => fruit),.....................
支持支持支持
不错的思路,代码蛮简洁
很好的方法
嗯嗯,不错哦!
不错,值得看看
好东西,收藏了
树状数据存在数据库中吗?
看了很久不明白,为什么要这样来查出数据?
直接用循环不就可以了?
难者不会,会者不难的啊!
学习,学习,谢谢
很好的资源.谢谢
初来乍到!回帖支持下
楼主幸苦了。谢谢分享此
好,呵呵呵呵呵
这是什么玩意
楼主幸苦了。值得学习
很好啊
真的很不错
学习,感谢楼主