php 读取树形结构的完整代码

前端之家收集整理的这篇文章主要介绍了php 读取树形结构的完整代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:
  1. /**
  2. * 从数组转换,操作无限分类数组.
  3. * 数组格式,array(array('cid','pid','val'),array('cid','val'));
  4. *
  5. * @param
  6. * @author 编程之家 jb51.cc jb51.cc
  7. **/
  8. class tree_array {
  9. var $data = array();
  10. var $child = array();
  11. var $layer = array();
  12. var $parent = array();
  13. // $array,被操纵的数组,$cid 分类 id 的key. $pid 父分类 id 的key. $value 数据的 key
  14. function tree_array($array = array(),$cid = 'cid',$pid = 'pid',$value = null) {
  15. if(!is_array($array)) return false;
  16. foreach($array as $v) {
  17. if(isset($v[$value])) {
  18. $this->setNode($v[$cid],$v[$pid],$v[$value]);
  19. }
  20. else
  21. {
  22. $this->setNode($v[$cid],$v);
  23. }
  24. }
  25. }
  26. function setNode($id,$parent,$value){
  27. $parent = $parent ? $parent : 0;
  28. $this->data[$id] = $value;
  29. // if(!isset($this->child[$id])) $this->child[$id] = array();
  30. if(!isset($this->child[$parent])) $this->child[$parent] = array();
  31. $this->child[$parent][] = $id;
  32. $this->parent[$id] = $parent;
  33. }
  34. function getValue($id) {
  35. if(!isset($this->data[$id])) return false;
  36. return $this->data[$id];
  37. }
  38. function getLayer($id,$space = false) {
  39. if(!isset($this->parent[$id])) return false;
  40. $layer = count($this->getParents($id)) + 1;
  41. return $space ? str_repeat($space,$layer) : $layer;
  42. }
  43. function getTreeList(&$tree,$root= 0,$space=null) {
  44. if(!isset($this->child[$root])) return false;
  45. foreach($this->child[$root] as $key=>$id) {
  46. if($space) {
  47. $tree[$id] = $this->getLayer($id,$space) . $this->data[$id];
  48. }
  49. else
  50. {
  51. $tree[$id] = $this->data[$id];
  52. }
  53. if(isset($this->child[$id])) $this->getTreeList($tree,$id,$space);
  54. }
  55. }
  56. function getParent($id) {
  57. if(!isset($this->parent[$id])) return false;
  58. $tid = $this->parent[$id];
  59. if(!$tid) return 0;
  60. return array($tid => $this->data[$tid]);
  61. }
  62. function getParents($id) {
  63. if(!isset($this->parent[$id])) return false;
  64. $parents = array();
  65. while($this->parent[$id]){
  66. $id = $this->parent[$id];
  67. $parents[$id] = $this->data[$id];
  68. }
  69. return $parents;
  70. }
  71. function getChild($id) {
  72. if(!isset($this->child[$id])) return false;
  73. $array = array();
  74. foreach($this->child[$id] as $v) {
  75. $array[$v] = $this->data[$v];
  76. }
  77. return $array;
  78. }
  79. function getChilds($id = 0) {
  80. if(!isset($this->child[$id])) return false;
  81. $child = array();
  82. $this->getTreeList($child,$id);
  83. return $child;
  84. }
  85. function html_options($id = 0,$space=' ',$layer=0) {
  86. static $layer;
  87. if(!isset($this->child[$id])) return false;
  88. $tree = array();
  89. foreach($this->child[$id] as $key=>$id) {
  90. if($space) {
  91. $tree[$id] =(str_repeat($space,$layer)) . $this->data[$id];
  92. }
  93. else
  94. {
  95. $tree[$id] = $this->data[$id];
  96. }
  97. if(isset($this->child[$id])) {
  98. $layer++;
  99. $tree += $this->html_options($id,$space,$layer);
  100. $layer--;
  101. }
  102. }
  103. return $tree;
  104. }
  105. }
  106. /*** 代码来自编程之家 jb51.cc(jb51.cc) ***/

猜你在找的PHP相关文章