首页 >php教程 >PHP源码 >thinkphp里面的无限级分类

thinkphp里面的无限级分类

PHP中文网
PHP中文网原创
2016-05-26 08:20:49951浏览

php代码

<?php

	Class Category{

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

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

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


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

		//通过父级寻找子级
		Static Public function findChild($cate,$pid){
			$arr=array();
			foreach($cate as $v){
				if($v[&#39;pid&#39;] == $pid){
					$arr[]=$v[&#39;id&#39;];
					$arr=array_merge($arr,self::findChild($cate,$v[&#39;id&#39;]));
				}
			}
			return $arr;
		}
	}
?>
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn