首頁  >  文章  >  php教程  >  thinkphp里面的无限级分类

thinkphp里面的无限级分类

PHP中文网
PHP中文网原創
2016-05-26 08:20:49929瀏覽

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