递归问题求解! 本帖最后由 jordan102 于 2011-03-11 14:51:51 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给你个php无限分类的树类吧 1. <?php 2. class tree { 3. var $data = array(); 4. var $child = array(-1 => array()); 5. var $layer = array(-1 => -1); 6. var $parent = array(); 7. var $countid = 0; 8. function tree() {} 9. 10. function setNode($id, $parent, $value) { 11. 12. $parent = $parent?$parent:0; 13. 14. $this->data[$id] = $value; 15. $this->child[$parent][] = $id; 16. $this->parent[$id] = $parent; 17. 18. if(!isset($this->layer[$parent])) { 19. $this->layer[$id] = 0; 20. } else { 21. $this->layer[$id] = $this->layer[$parent] + 1; 22. } 23. } 24. 25. function getList(&$tree, $root= 0) { 26. foreach($this->child[$root] as $key => $id) { 27. $tree[] = $id; 28. if($this->child[$id]) $this->getList($tree, $id); 29. } 30. } 31. 32. function getValue($id) { 33. return $this->data[$id]; 34. } 35. 36. function reSetLayer($id) { 37. if($this->parent[$id]) { 38. $this->layer[$this->countid] = $this->layer[$this->countid] + 1; 39. $this->reSetLayer($this->parent[$id]); 40. } 41. } 42. 43. function getLayer($id, $space = false) { 44. //重新计算级数 45. $this->layer[$id] = 0; 46. $this->countid = $id; 47. $this->reSetLayer($id); 48. return $space?str_repeat($space, $this->layer[$id]):$this->layer[$id]; 49. } 50. 51. function getParent($id) { 52. return $this->parent[$id]; 53. } 54. 55. function getParents($id) { 56. while($this->parent[$id] != -1) { 57. $id = $parent[$this->layer[$id]] = $this->parent[$id]; 58. } 59. 60. ksort($parent); //按照键名排序 61. reset($parent); //数组指针移回第一个单元 62. 63. return $parent; 64. } 65. 66. function getChild($id) { 67. return $this->child[$id]; 68. } 69. 70. function getChilds($id = 0) { 71. $child = array(); 72. $this->getList($child, $id); 73. 74. return $child; 75. } 76. } 77. 78. ?> 看图片上那个下拉框,当前是点击linux相册。我想让linux相册和三个子类都不显示!有什么办法吗? php 正则替换的问题 哪位大咖知道这段代码是做什么的? 关于用Dreamweaver编辑PHP和HTML的混编来添加背景图片 如何整合jquery fullcalendar到网站中 有北京的程序员想接私活吗? 为什么不能动态显示图片?达人请进 请问 php 中@ 代表什么含义 请问一个网站收多少钱,有网上定单,留言板等!谢谢! 我找不到办法解决!谢谢帮忙! PHP7缺少php_mysql.dll 期待高手解惑:无限二级域名的设置 php下载,弹出文件下载对话框,如果取消则终止操作不扣除积分,如何实现?
2. class tree {
3. var $data = array();
4. var $child = array(-1 => array());
5. var $layer = array(-1 => -1);
6. var $parent = array();
7. var $countid = 0;
8. function tree() {}
9.
10. function setNode($id, $parent, $value) {
11.
12. $parent = $parent?$parent:0;
13.
14. $this->data[$id] = $value;
15. $this->child[$parent][] = $id;
16. $this->parent[$id] = $parent;
17.
18. if(!isset($this->layer[$parent])) {
19. $this->layer[$id] = 0;
20. } else {
21. $this->layer[$id] = $this->layer[$parent] + 1;
22. }
23. }
24.
25. function getList(&$tree, $root= 0) {
26. foreach($this->child[$root] as $key => $id) {
27. $tree[] = $id;
28. if($this->child[$id]) $this->getList($tree, $id);
29. }
30. }
31.
32. function getValue($id) {
33. return $this->data[$id];
34. }
35.
36. function reSetLayer($id) {
37. if($this->parent[$id]) {
38. $this->layer[$this->countid] = $this->layer[$this->countid] + 1;
39. $this->reSetLayer($this->parent[$id]);
40. }
41. }
42.
43. function getLayer($id, $space = false) {
44. //重新计算级数
45. $this->layer[$id] = 0;
46. $this->countid = $id;
47. $this->reSetLayer($id);
48. return $space?str_repeat($space, $this->layer[$id]):$this->layer[$id];
49. }
50.
51. function getParent($id) {
52. return $this->parent[$id];
53. }
54.
55. function getParents($id) {
56. while($this->parent[$id] != -1) {
57. $id = $parent[$this->layer[$id]] = $this->parent[$id];
58. }
59.
60. ksort($parent); //按照键名排序
61. reset($parent); //数组指针移回第一个单元
62.
63. return $parent;
64. }
65.
66. function getChild($id) {
67. return $this->child[$id];
68. }
69.
70. function getChilds($id = 0) {
71. $child = array();
72. $this->getList($child, $id);
73.
74. return $child;
75. }
76. }
77.
78. ?>