Home  >  Article  >  Backend Development  >  PHP Infinitus Classification

PHP Infinitus Classification

WBOY
WBOYOriginal
2016-07-29 09:14:17721browse

Infinitus classification: recursive and iterative implementation

1. Find the sub-column of the specified column

2. Find the descendant columns of the specified column, which is a descendant tree

3. Find the parent directory, grandparent directory,... .,, top directory

欲处理的原始数据
$area=array(
array('id'=>1,'name'=>'山东','parent'=>0),
array('id'=>2,'name'=>'菏泽','parent'=>1),
array('id'=>3,'name'=>'定陶','parent'=>2),
array('id'=>4,'name'=>'青岛','parent'=>1),
array('id'=>5,'name'=>'莱西','parent'=>4),
array('id'=>6,'name'=>'烟台','parent'=>1),
array('id'=>7,'name'=>'南山','parent'=>6),
array('id'=>8,'name'=>'仿山','parent'=>3),
array('id'=>9,'name'=>'日照','parent'=>1),
array('id'=>10,'name'=>'济南','parent'=>1)

);

//找子栏目
function findSon($arr,$id=0){
	//查找$arr中元素的parent的值等于$id就是其子目录
	$s
	foreach ($arr as $v) {
	 	if($v['parent']==$id){
	 		$sons[]=$v;
	 		}
	} 
	return $sons;
}
//找子孙树
//利用<strong>静态变量</strong>
function findTree($arr,$id,$lev=1){
	static $subs=array();
	
	foreach ($arr as $v) {
	 	if($v['parent']==$id){
	 		$v['lev']=$lev;
	 		$subs[]=$v;
	 	    findTree($arr,$v['id'],$lev+1);
	 		}
	} 
	return $subs;
	
	}
方法二、
function findTree($arr,$id,$lev=1){
	 static $subs=array();
	
	foreach ($arr as $v) {
	 	if($v['parent']==$id){
	 		$v['lev']=$lev;
	 		$subs[]=$v;
	 	    $subs=$subs+findTree($arr,$v['id'],$lev+1);
	 		}
	} 
	return $subs;
	
	}
方法三、
function findTree($arr,$id,$lev=1){
	  $subs=array();
	
	foreach ($arr as $v) {
	 	if($v['parent']==$id){
	 		$v['lev']=$lev;
	 		$subs[]=$v;
	 	    $subs= array_merge( $subs,findTree($arr,$v['id'],$lev+1));
	 		}
	} 
	return $subs;
	
	}
//用迭代法来找子孙树

function subTree($arr,$parent=0){
	$task=array($parent);//任务表
	$tree=array();//地区表
	while(!empty($task)){
		
		$flag=false;
		foreach($arr as $k=>$v){
			
			if($v['parent']==$parent){
				$tree[]=$v;
				array_push($task,$v['id']);//最新的地区id入任务栈
				$parent=$v['id'];
				unset($arr[$k]);//把找到单元unset
				
				$flag=true;
				}
				
			}
			if($flag==false){
					
					array_pop($task);
					$parent=end($task);
		}
		}
		
	return $tree;
		
	}

//===
//无限极分类
//家谱树
//===
方法一、
function familyTree($arr,$id,$lev=1){
	
	//static $tree=array();
	 $tree=array();
	foreach($arr as $v){
		
	 	if($v['id']==$id){
	 		$v['lev']=$lev;
	 		
	 		//判断改子类是否存在父目录
	 		if($v['parent'] > 0){
	 			$tree=array_merge($tree,familyTree($arr,$v['parent'],$lev+1));
	 			//familyTree($arr,$v['parent']);
	 			
	 			
	 			}
	 			
	 			$tree[]=$v;
	 		}
	} 
	return $tree;
	}
方法二、
//用迭代法来找家谱树

function tree($arr,$id){
	$tree=array();

	while($id !==0){
		foreach ($arr as $v) {
			
		    if($v['id']==$id){
		    	
		    	$tree[]=$v;
		    	$id=$v['parent'];
		    	break;
		    	}
	        
		}
		
		}
 
	
	return $tree;
	}

The above has introduced the PHP Infinitus classification, including static variables. I hope it will be helpful to friends who are interested in PHP tutorials.

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