<?php class MyCate{ /** * 组合成一维数组 * @param $cateArr 数组 * @param string $html * @param int $pid 上级ID * @param int $level 等级 * @return array */ static function unLimitedForLevel($cateArr,$html=' -- ',$pid=0,$level=0){ $arr = array(); foreach($cateArr as $v){ if($v['pid']==$pid){ $v['level'] = $level+1; $v['html'] = str_repeat($html,$level); $arr[] = $v; $arr = array_merge($arr,self::unLimitedForLevel($cateArr,$html,$v['id'],$level+1)); } } return $arr; } /** * 无限级分类,组合成多级数组 * @param $cateArr 分类数组 * @param int $pid 上级ID * @return array */ static function unLimitForLayer($cateArr,$pid=0){ $arr = array(); foreach($cateArr as $v){ if($v['pid']==$pid){ $v['child'] = self::unLimitForLayer($cateArr,$v['id']); $arr[] = $v; } } return $arr; } /** * 给自ID,得到所属父级ID数据 * @param $cateArr 数组 * @param $id 自身ID,递归中传PID * @return array */ static function getParents($cateArr,$id){ $arr = array(); foreach($cateArr as $v){ if($v['id']==$id){ $arr[] = $v; #$arr = array_merge($arr,self::getParents($cateArr,$v['pid'])); $arr = array_merge(self::getParents($cateArr,$v['pid']),$arr); } } return $arr; } /** * 传递一个父级分类ID返回所有子分类ID */ static function getChildsId($cateArr,$pid){ $arr = array(); foreach($cateArr as $v){ if($v['pid']==$pid){ $arr[] = $v['id']; $arr = array_merge($arr,self::getChildsId($cateArr,$v['id'])); } } return $arr; } }
# 递归和迭代的应用
<?php header('Content-Type:text/html;charset=UTF-8'); /** * 递归打印级联目录 */ function recDir($path,$level=1){ $dh = opendir($path); while (false !== ($dir = readdir($dh))) { if($dir=='.' || $dir=='..') continue; echo str_repeat(' ', $level).$dir.'<hr>'; if(is_dir($path.'/'.$dir)){ recDir($path.'/'.$dir,$level+1); } } closedir($dh); } #recDir('./pmd/'); // 递归创建目录 function mk_dir($path){ # 目录要是直接存在,返回true if(is_dir($path)) return true; # 判断目录的父目录是否存在,存在就可以直接创建 if(is_dir(dirname($path))) return mkdir($path); # 如果父目录也不存在,创建父目录 mk_dir(dirname($path)); return mkdir($path); } #var_dump(mk_dir('./a/b/c/d/f/g/i')); // 递归创建目录方法2 function mk_dir2($path){ # 如果目录存在,直接返回 if(is_dir($path)) return true; #如果目录不存在,创建 return is_dir(dirname($path)) || mk_dir2(dirname($path))? mkdir($path) : false; } #var_dump(mk_dir2('./aa/b/c/d/f/g/i')); // 迭代创建级联目录 function mk_dir3($path){ $arr = array(); while(!is_dir($path)){ array_unshift($arr, $path); $path = dirname($path); } if(count($arr)<=0) return true; foreach($arr as $v){ mkdir($v); } return true; } mk_dir3('./aa/b/c/d/f/g/i'); // 递归删除目录 function delDir($path){ # 不是目录直接返回 if(!is_dir($path)) return false; $dh = opendir($path); while(false !== ($row=readdir($dh))){ if($row=='.' || $row=='..') continue; # 是否是普通文件 if(!is_dir($path.'/'.$row)){ unlink($path.'/'.$row); }else{ delDir($path.'/'.$row); } } closedir($dh); rmdir($path); return true; } $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'=>2), array('id'=>5,'name'=>'朝阳','parent'=>1), array('id'=>6,'name'=>'昌平','parent'=>1), array('id'=>7,'name'=>'顺义','parent'=>1), array('id'=>8,'name'=>'安徽','parent'=>0), array('id'=>9,'name'=>'芜湖','parent'=>8) ); // 找子栏目 function subTree($arr,$parentID=0,$level=1){ static $sonArr = array(); foreach($arr as $v){ if($parentID == $v['parent']){ $v['level'] = $level; $sonArr[] = $v; subTree($arr,$v['id'],$level+1); } } return $sonArr; } function subTree2($arr,$parentID=0,$level=1){ $sonArr = array(); foreach($arr as $v){ if($parentID == $v['parent']){ $v['sub'] = subTree2($arr,$v['id'],$level+1); $sonArr[] = $v; } } return $sonArr; } /* $a = subTree2($area,0); echo '<pre>'; print_r($a); */ function parentTree($area,$id){ static $tree = array(); foreach($area as $v){ if($v['id']==$id){ $tree[] = $v; if($v['parent']>0){ parentTree($area,$v['parent']); } } } return $tree; } function parentTree2($area,$id){ $tree = array(); foreach($area as $v){ if($v['id']==$id){ $tree[] = $v; if($v['parent']>0){ $tree = array_merge(parentTree2($area,$v['parent']),$tree); #$tree = array_merge($tree,parentTree2($area,$v['parent'])); } } } return $tree; } /* $a = parentTree2($area,3); echo '<pre>'; print_r($a); */ // 迭代Tree操作 function iterationTree($arr,$id){ $tree = array(); while($id!==0){ foreach($arr as $v){ if($v['id'] == $id){ $tree[] = $v; $id = $v['parent']; break; } } } return $tree; } /* $a = iterationTree($area,3); echo '<pre>'; print_r($a); */ // 用迭代法找子孙树 function iterationSubTree($arr,$parent=0){ $task = array($parent); # 任务表 $tree = array(); # 地区表 while (count($task)>0) { $flag = false; foreach($arr as $k=>$v){ if($v['parent']==$parent){ $tree[] = $v; # 最新的地区ID入任务栈 array_push($task, $v['id']); $parent = $v['id']; unset($arr[$k]); $flag = true; # 说明找到了子栏目 break; } } if($flag == false){ array_pop($task); $parent = end($task); } } return $tree; } $a = iterationSubTree($area,0); echo '<pre>'; print_r($a); ?>