PHP无限分类(树形类)的深入分析
krsort($tmp);
for ($i = count($tmp); $i > 0; $i--) {
foreach ($tmp as $k => $v) {
if (!in_array($k, $this->already)) {
if (!$this->tmp) {
$this->tmp = array($k, $v);
$this->already[] = $k;
continue;
} else {
foreach ($v as $key => $value) {
if ($value[$this->fields[0]] == $this->tmp[0]) {
$tmp[$k][$key]['child'] = $this->tmp[1];
$this->tmp = array($k, $tmp[$k]);
}
}
}
}
}
$this->tmp = null;
}
$this->tmp = $tmp;
}
/**
* 反向递归
*/
private function recur_n($arr, $id) {
foreach ($arr as $v) {
if ($v[$this->fields[0]] == $id) {
$this->arr[] = $v;
if ($v[$this->fields[1]] != $this->root) $this->recur_n($arr, $v[$this->fields[1]]);
}
}
}
/**
* 正向递归
*/
private function recur_p($arr) {
foreach ($arr as $v) {
$this->arr[] = $v[$this->fields[0]];
if ($v['child']) $this->recur_p($v['child']);
}
}
/**
* 菜单 多维数组
*
* @param integer $id 分类id
* @return array 返回分支,默认返回整个树
*/
public function leaf($id = null) {
$id = ($id == null) ? $this->root : $id;
return $this->tmp[$id];
}
/**
* 导航 一维数组
*
* @param integer $id 分类id
* @return array 返回单线分类直到顶级分类
*/
public function navi($id) {
$this->arr = null;
$this->recur_n($this->result, $id);
krsort($this->arr);
return $this->arr;
}
/**
* 散落 一维数组
*
* @param integer $id 分类id
* @return array 返回leaf下所有分类id
*/
public function leafid($id) {
$this->arr = null;
$this->arr[] = $id;
$this->recur_p($this->leaf($id));
return $this->arr;
}
}
?>
【PHP无限分类(树形类)的深入分析】相关文章:
php无限分类方法讲解09-30
PHP排序算法类讲解09-30
php调用父类方法09-29
PHP经典常用特效类代码09-11
PHP类和对象的相关函数讲解10-01
PHP中的类与对象入门知识09-13
PHP封装数据库操作类09-26
PHP新手之学习类与对象09-14
PHP编程:类和对象、方法调用09-12
php备份数据库类的方法09-30