无限级分类应用:
1 找指定栏目的子孙栏目
2 找指定栏目的父栏目、父父栏目
';
//无限级分类
$area = array(
array('id'=>1,'name'=>'安徽','parent'=>0),
array('id'=>2,'name'=>'海淀','parent'=>7),
array('id'=>3,'name'=>'濉溪县','parent'=>5),
array('id'=>4,'name'=>'昌平','parent'=>7),
array('id'=>5,'name'=>'淮北','parent'=>1),
array('id'=>6,'name'=>'朝阳','parent'=>7),
array('id'=>7,'name'=>'北京','parent'=>0),
array('id'=>8,'name'=>'上地','parent'=>2),
);
//查找子孙栏目
function findSun($arr,$id){
$sons = array();
foreach($arr as $v){
if($v['parent']==$id){
$sons[] = $v;
}
}
return $sons;
}
//查找子孙树
function subTree($arr,$id=0,$lev=1){
static $subs = array();
foreach($arr as $v){
if($v['parent']==$id){
$v['lev']=$lev;
$subs[] = $v;
subTree($arr,$v['id'],$lev+1);
}
}
return $subs;
}
/*
//打印子孙树
$tree = subTree($area,0);
foreach($tree as $k=>$v){
echo str_repeat(' ',$v['lev']),$v['name'],'
';
}
*/
//查找父栏目
function familyTree($arr,$id){
static $tree = array();
foreach($arr as $v){
if($v['id']==$id){
if($v['parent']>0){
familyTree($arr,$v['parent']);
}
$tree[] = $v;
}
}
return $tree;
}
print_r(familyTree($area,8));
?>
递归转义:
$v){
if(is_string($v)){
$arr[$k]=addslashes($v);
}else if(is_array($v)){
$arr[$k]=_addslashes($v);
}
}
return $arr;
}
?>