一、递归

  1. //递归
  2. //树形的类
  3. class Tree
  4. {
  5. protected $data;
  6. public $treeList;
  7. public function __construct()
  8. {
  9. $catalog= new Catalog();//实例化分类的model
  10. $this->data=$catalog->show();//将要分类的数据查询出来
  11. $this->treeList=$this->tree($result,0,0);//构造中就执行了分类方法
  12. }
  13. /**
  14. * $result为存放数据的空数组
  15. * $pid 为数据表字段中的pid 初始为0 顶级类别先查询
  16. * $spac 为计数器
  17. */
  18. public function tree(&$result=array(),$pid,$spac){
  19. $spac=$spac+4;
  20. foreach($this->data as $value){
  21. if($value['p_id']==$pid){
  22. $value['catalog_name'] = str_repeat(' ',$spac)."|--".$value['catalog_name'];
  23. $result[] = $value;
  24. unset($value); //把这个节点从数组中移除,减少后续递归消耗
  25. $this->tree($result,$value['catalog_id'],$spac);
  26. }
  27. }
  28. return $result;
  29. }
  30. }
  31. //其他界面调用的代码
  32. $tree=new Tree();
  33. $tree_list=$tree->treeList;

二、引用

  1. //假设这是我们数据库查询出来的数据(二维数组)
  2. $arr=[
  3. [
  4. 'tid' => 1,
  5. 'tname' => '亚洲',
  6. 'p_id' =>0],
  7. [
  8. 'tid' => 2,
  9. 'tname' => '欧洲' ,
  10. 'p_id' => 0],
  11. [
  12. 'tid' => 3,
  13. 'tname' => '非洲' ,
  14. 'p_id' => 0],
  15. [
  16. 'tid' => 4,
  17. 'tname' => '中国',
  18. 'p_id' => 1],
  19. [
  20. 'tid' => 5,
  21. 'tname' => '美洲',
  22. 'p_id' => 0],
  23. [
  24. 'tid' => 6,
  25. 'tname' => '台湾',
  26. 'p_id' => 4],
  27. [
  28. 'tid' => 7,
  29. 'tname' => '墨西哥' ,
  30. 'p_id' => 5]
  31. ];
  32. function generateTree($array){
  33. //第一步 构造数据 为上面的数组加上索引
  34. $items = array();
  35. foreach($array as $value){
  36. $items[$value['tid']] = $value;
  37. }
  38. //第二步 遍历数据 生成树状结构
  39. $tree = array();
  40. foreach($items as $key => &$value){
  41. if(isset($items[$value['p_id']])){
  42. $items[$value['p_id']]['son'][] = &$value;
  43. }else{
  44. $tree[] = &$value;
  45. }
  46. //如果还涉及到另外一张表,另一张表的id与分类表的id一样时
  47. //将分类和标签结合
  48. //foreach($tags as $k=>$v){
  49. //if ($v['catalog_id']==$value['catalog_id']){
  50. //$value['tags'][]=$v;
  51. //}
  52. //}
  53. }
  54. return $tree;
  55. }
  56. //测试代码
  57. $list=generateTree($arr);
  58. print_r($list);
  59. //测试的结果
  60. Array
  61. (
  62. [0] => Array
  63. (
  64. [tid] => 1
  65. [tname] => 亚洲
  66. [p_id] => 0
  67. [son] => Array
  68. (
  69. [0] => Array
  70. (
  71. [tid] => 4
  72. [tname] => 中国
  73. [p_id] => 1
  74. [son] => Array
  75. (
  76. [0] => Array
  77. (
  78. [tid] => 6
  79. [tname] => 台湾
  80. [p_id] => 4
  81. )
  82. )
  83. )
  84. )
  85. )
  86. [1] => Array
  87. (
  88. [tid] => 2
  89. [tname] => 欧洲
  90. [p_id] => 0
  91. )
  92. [2] => Array
  93. (
  94. [tid] => 3
  95. [tname] => 非洲
  96. [p_id] => 0
  97. )
  98. [3] => Array
  99. (
  100. [tid] => 5
  101. [tname] => 美洲
  102. [p_id] => 0
  103. [son] => Array
  104. (
  105. [0] => Array
  106. (
  107. [tid] => 7
  108. [tname] => 墨西哥
  109. [p_id] => 5
  110. )
  111. )
  112. )
  113. )

拓展:根据标签查找所属的父级分类(pid=0)

2张表(分类表和标签表)

sp_type 分类表 —tid —tname 分类名称 —p_id 父级id


sp_tags 标签表 —tag_id —tag_name 标签名称 —tid 对应的分类表id

需求:传入tag_id 数组,查询对应的分类表最顶级分类 p_id=0

  1. $tag = input('');
  2. $tags_res = Db::name('tags')
  3. ->where('tag_id', 'in', $tag['tags_id'])
  4. ->join('sp_type', 'sp_type.tid=sp_tags.tid')
  5. ->select();
  6. //最初结果
  7. array (size=2)
  8. 0 =>
  9. array (size=5)
  10. 'tag_id' => int 2
  11. 'tag_name' => string '非洲大草原' (length=15)
  12. 'tid' => int 2
  13. 'tname' => string '非洲' (length=6)
  14. 'p_id' => int 0
  15. 1 =>
  16. array (size=5)
  17. 'tag_id' => int 7
  18. 'tag_name' => string '泉州' (length=6)
  19. 'tid' => int 6
  20. 'tname' => string '福建' (length=6)
  21. 'p_id' => int 20
  22. ----------------------------------------------------------------------
  23. for ($i = 0; $i < count($tags_res); $i++) {
  24. $res[] = $this->getZero($tags_res[$i]);
  25. }
  26. public function getZero($param)
  27. {
  28. $type = Db::name('type')->select();
  29. while (true) {
  30. if ($param['p_id'] == 0) {
  31. return $param;
  32. }
  33. foreach ($type as $key => $value) {
  34. if ($value['tid'] == $param['p_id']) {
  35. $param=[
  36. 'tid'=>$value['tid'],
  37. 'tname'=>$value['tname'],
  38. 'tag_name'=>$param['tag_name'],
  39. 'p_id'=>$value['p_id']
  40. ];
  41. continue;
  42. }
  43. }
  44. }
  45. }
  46. //执行结果,获得传入的标签数组对应的分类数组的顶级分类
  47. array (size=2)
  48. 0 =>
  49. array (size=5)
  50. 'tag_id' => int 2
  51. 'tag_name' => string '非洲大草原' (length=15)
  52. 'tid' => int 2
  53. 'tname' => string '非洲' (length=6)
  54. 'p_id' => int 0
  55. 1 =>
  56. array (size=4)
  57. 'tid' => int 1
  58. 'tname' => string '亚洲' (length=6)
  59. 'tag_name' => string '泉州' (length=6)
  60. 'p_id' => int 0