Home  >  Article  >  php教程  >  thinkphp里面的无限级分类

thinkphp里面的无限级分类

PHP中文网
PHP中文网Original
2016-05-26 08:20:49931browse

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;
		}
	}
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn