第一步:在components 新建Tree.php文件
第二步:在Controller的actionIndex方法里面执行create方法<?php class Tree { /** * @param array $param * $param string $pid * return array $param */ static public $treeList=array(); public function create($data,$pid=0){ foreach($data as $key=>$value){ if($value['pid']==$pid){ self::$treeList[]=$value; unset($data[$key]); self::create($data,$value['id']); } } return self::$treeList; } }
public function actionIndex(){
parent::_acl();
$school = new Schoolattr;
$where = "select * from un_school_attr where countries_type = 1";
$schoolList = $school->findAllBySql($where);
$tree = new Tree;
$schoolList = $tree::create($schoolList);
print_r($schoolList); exit;
}
本文介绍了一种使用PHP实现树形结构的方法。首先定义了一个Tree类,并在该类中创建了create方法来递归地构建树形数组。然后在控制器的actionIndex方法中调用此方法,展示如何从数据库获取数据并构建出树形结构。

5532

被折叠的 条评论
为什么被折叠?



