sql:
sql内容:
model:AddonGolfArticleCate.php
public function listToTree($lists, $childKey = 'children'){
$map = [];
$res = [];
foreach($lists as $id => &$item){
$pid = &$item['pid'];
$map[$item['id']] = &$item;
if(!isset($map[$pid])){
$res[$id] = &$item;
}else{
$pItem = &$map[$pid];
$pItem[$childKey][] = &$item;
}
}
return $res;
}
控制器:
$articleCate=AddonGolfArticleCate::find()->select('*')->asArray()->all();
$cates=new AddonGolfArticleCate();
$tree=$cates->listToTree($articleCate);
$trees=array_column($tree,null);
return $this->render("introduction",[
'trees'=>$trees,
'articleCates'=>$articleCates
]);
视图: 一级菜单:
$val['title']
二级菜单:
$vall['title]
三级菜单:
$valll['title']