/**
* 把返回的数据集转换成Tree
* @access public
* @param array $list 要转换的数据集
* @param string $pid parent标记字段
* @param string $level level标记字段
* @return array
*/
function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0) {
// 创建Tree
$tree = array();
if (is_array($list)) {
// 创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data) {
$refer[$data[$pk]] = & $list[$key];
}
foreach ($list as $key => $data) {
// 判断是否存在parent
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] = & $list[$key];
} else {
if (isset($refer[$parentId])) {
$parent = & $refer[$parentId];
$parent[$child][] = & $list[$key];
}
}
}
}
return $tree;
}总是提示:_child错误。 /**
* 栏目管理
* @return mixed
*/
public function index() {
$this->assign('title', '栏目管理');
$datalist = $this->category_model->order('px asc,id desc')->select();
$datalist = list_to_tree($datalist,'id','pid'); //实现无级排序
print_r($datalist);
die;
//$datalist = $this->category_model->findchild($datalist);
$this->assign('datalist', $datalist);
return $this->fetch('index');
}
protected function findChild($arr){
static $tree=array();
foreach ($arr as $key=>$val){
$tree[]=$val;
if (isset($val['_child'])){
$this->findChild($val['_child']);
}
}
return $tree;
} 最佳答案