tree.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Fanwe 方维直播系统
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: 云淡风轻(1956838968@qq.com)
  8. // +----------------------------------------------------------------------
  9. class tree{
  10. /**
  11. +----------------------------------------------------------
  12. * 把返回的数据集转换成Tree
  13. +----------------------------------------------------------
  14. * @access public
  15. +----------------------------------------------------------
  16. * @param array $list 要转换的数据集
  17. * @param string $pid parent标记字段
  18. * @param string $level level标记字段
  19. +----------------------------------------------------------
  20. * @return array
  21. +----------------------------------------------------------
  22. */
  23. public function toTree($list, $pk='id',$pid = 'pid',$child = '_child')
  24. {
  25. // 创建Tree
  26. $tree = array();
  27. if(is_array($list)) {
  28. // 创建基于主键的数组引用
  29. $refer = array();
  30. foreach ($list as $key => $data) {
  31. $_key = is_object($data)?$data->$pk:$data[$pk];
  32. $refer[$_key] =& $list[$key];
  33. }
  34. foreach ($list as $key => $data) {
  35. // 判断是否存在parent
  36. $parentId = is_object($data)?$data->$pid:$data[$pid];
  37. $is_exist_pid = false;
  38. foreach($refer as $k=>$v)
  39. {
  40. if($parentId==$k)
  41. {
  42. $is_exist_pid = true;
  43. break;
  44. }
  45. }
  46. if ($is_exist_pid) {
  47. if (isset($refer[$parentId])) {
  48. $parent =& $refer[$parentId];
  49. $parent[$child][] =& $list[$key];
  50. }
  51. } else {
  52. $tree[] =& $list[$key];
  53. }
  54. }
  55. }
  56. return $tree;
  57. }
  58. /**
  59. * 将格式数组转换为树
  60. *
  61. * @param array $list
  62. * @param integer $level 进行递归时传递用的参数
  63. */
  64. private $formatTree; //用于树型数组完成递归格式的全局变量
  65. private function _toFormatTree($list,$level=0,$title = 'title')
  66. {
  67. foreach($list as $key=>$val)
  68. {
  69. $tmp_str=str_repeat("&nbsp;&nbsp;",$level*2);
  70. $tmp_str.="";
  71. $val['level'] = $level;
  72. $val['title_show'] = $tmp_str.$val[$title];
  73. if(!array_key_exists('_child',$val))
  74. {
  75. array_push($this->formatTree,$val);
  76. }
  77. else
  78. {
  79. $tmp_ary = $val['_child'];
  80. unset($val['_child']);
  81. array_push($this->formatTree,$val);
  82. $this->_toFormatTree($tmp_ary,$level+1,$title); //进行下一层递归
  83. }
  84. }
  85. return;
  86. }
  87. public function toFormatTree($list,$title = 'title')
  88. {
  89. $list = $this->toTree($list);
  90. $this->formatTree = array();
  91. $this->_toFormatTree($list,0,$title);
  92. return $this->formatTree;
  93. }
  94. public function toNameFormatTree($list,$title = 'name')
  95. {
  96. $list = $this->toTree($list);
  97. $this->formatTree = array();
  98. $this->_toFormatTree($list,0,$title);
  99. return $this->formatTree;
  100. }
  101. }
  102. ?>