欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

thinkphp里面的无限级分类

程序员文章站 2022-05-23 15:52:42
...
php代码
<?php

	Class Category{

		//一维数组无限级分类
		Static Public function yiwei($cate,$pid=0){

			$arr=array();
			foreach($cate as $v){
				if($v['pid'] == $pid){
					$arr[]=$v;
					$arr=array_merge($arr,self::yiwei($cate,$v['id']));
				}
				
			}
			return $arr;
		}

		//二位数组无限级分类
		Static Public function erwei($cate,$pid=0){
			$arr=array();
			foreach($cate as $v){
				if($v['pid'] == $pid){
					
					$v['child']=self::erwei($cate,$v['id']);
					$arr[]=$v;
				}
			}
			return $arr;
		}


		//通过子级寻找父级
		Static Public function findFather($cate,$id){
			$arr=array();
			foreach($cate as $v){
				if($v['id'] == $id){
					$arr[]=$v;
					$arr=array_merge($arr,self::findFather($cate,$v['pid']));
				}
			}
			return $arr;
		}

		//通过父级寻找子级
		Static Public function findChild($cate,$pid){
			$arr=array();
			foreach($cate as $v){
				if($v['pid'] == $pid){
					$arr[]=$v['id'];
					$arr=array_merge($arr,self::findChild($cate,$v['id']));
				}
			}
			return $arr;
		}
	}
?>