首先,我返回一个数组出来。
id , uid , title
递归是用uid来区分的
例如:
id uid title
1 0 首页
2 1 新闻
3 1 产品
那么问题来了, 我在网上找了一个递归树型的类。 一直用得挺好。今天发现一个问题。请高手帮我解决一下。
就是这个 类 在处理递归的时候。 他是按照ID顺序来的
比如:id=1 uid=0
id=2 uid=1
id=3 uid=2
id=4 uid=0
它是这样递归下去的。 如果我返回的数组不是按ID排序的。 这个类 就无法递归了
比如我把 id=2 移动到 Id=4下面去
就会返回id=1 uid=0
id=4 uid=0
id=2 uid=4
之后不管有多少 uid=2的子分类 都不会在显示了。 不知道我这么表答清楚吗。谢谢大家。class Tree {
private $result;
private $tmp;
private $arr;
private $already = array();
/**
* 构造函数
*
* @param array $result 树型数据表结果集
* @param array $fields 树型数据表字段,array(分类id,父id)
* @param integer $root 顶级分类的父id
*/
public function __construct($result, $fields = array('id', 'uid'), $root = 0) {
$this->result = $result;
$this->fields = $fields;
$this->root = $root;
$this->handler();
}
/**
* 树型数据表结果集处理
*/
private function handler() {
foreach ($this->result as $node) {
$tmp[$node[$this->fields[1]]][] = $node;
}
@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($arr, $id) {
foreach ($arr as $v) {
if ($v[$this->fields[0]] == $id) {
$this->arr[] = $v;
if ($v[$this->fields[1]] != $this->root) $this->recur($arr, $v['uid']);
}
}
}
/**
* 菜单 多维数组
*
* @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->recur($this->result, $id);
krsort($this->arr);
return $this->arr;
}
}
$op = new Tree(我的数组);
$arr = $op -> leaf();我返回的数组 决对没有问题的
id , uid , title
递归是用uid来区分的
例如:
id uid title
1 0 首页
2 1 新闻
3 1 产品
那么问题来了, 我在网上找了一个递归树型的类。 一直用得挺好。今天发现一个问题。请高手帮我解决一下。
就是这个 类 在处理递归的时候。 他是按照ID顺序来的
比如:id=1 uid=0
id=2 uid=1
id=3 uid=2
id=4 uid=0
它是这样递归下去的。 如果我返回的数组不是按ID排序的。 这个类 就无法递归了
比如我把 id=2 移动到 Id=4下面去
就会返回id=1 uid=0
id=4 uid=0
id=2 uid=4
之后不管有多少 uid=2的子分类 都不会在显示了。 不知道我这么表答清楚吗。谢谢大家。class Tree {
private $result;
private $tmp;
private $arr;
private $already = array();
/**
* 构造函数
*
* @param array $result 树型数据表结果集
* @param array $fields 树型数据表字段,array(分类id,父id)
* @param integer $root 顶级分类的父id
*/
public function __construct($result, $fields = array('id', 'uid'), $root = 0) {
$this->result = $result;
$this->fields = $fields;
$this->root = $root;
$this->handler();
}
/**
* 树型数据表结果集处理
*/
private function handler() {
foreach ($this->result as $node) {
$tmp[$node[$this->fields[1]]][] = $node;
}
@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($arr, $id) {
foreach ($arr as $v) {
if ($v[$this->fields[0]] == $id) {
$this->arr[] = $v;
if ($v[$this->fields[1]] != $this->root) $this->recur($arr, $v['uid']);
}
}
}
/**
* 菜单 多维数组
*
* @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->recur($this->result, $id);
krsort($this->arr);
return $this->arr;
}
}
$op = new Tree(我的数组);
$arr = $op -> leaf();我返回的数组 决对没有问题的
若数据为数组,则并不需要递归(当然递归也可以)
你的代码使用了 krsort 函数,显然是不妥当的
因为不应该有子节点id 一定要大于父节点id 这样的假定
这个类 我是在网上找的
我只想按UID来指序。不想按ID 正序排列
<?php
function array2tree($node,$root=0){
$arr = array();
foreach ($node as $v) {
if ($v['parentid'] == $root) {
$v['child']=array2tree($node,$v['id']);
$arr[]=$v;
}
}
return $arr;
}
?>