标签:== empty 无限分类 无限 框架 each list func main
**算法不一定是首创,但一定是原创哈!我自己给这个算法取个名字,叫“一致无限分类法”。。哈哈!**
奉上代码:(使用的thinkphp框架)
public function menu(){
$data = M(‘menu‘) -> select(); //获取数据
$data = $this -> index_to_key($data,‘menu_id‘); //把primary key转移到数组索引上去
foreach($data as &$one){
$one[‘children‘] = $this -> search_children($one[‘menu_id‘],$data); //查询子节点
}
$this -> assign(‘data‘,$data); //前端显示
$this -> display();
echo ‘<pre>‘;
print_r($data);
}
//查询子节点并返回
function search_children($id,$data){
$children = array();
foreach($data as $one){
if($one[‘menu_pid‘] == $id){
$children[] = $one[‘menu_id‘];
}
}
return $children;
}
//将数据的键名提到数组索引上去
function index_to_key($data,$main_key){
$new_data = array();
foreach($data as $row){
$new_data[$row[$main_key]] = $row;
}
return $new_data;
}
前端代码:
<ul>
<foreach name="data" item="v1" key="k1">
<eq name="v1[‘menu_pid‘]" value="0">
<li>{$v1.menu_name}</li>
<notempty name="v1[‘children‘]">
<ul>
<foreach name="v1[‘children‘]" item="v2" key="k2">
<li>{$data[$v2][‘menu_name‘]}</li>
<notempty name="data[$v2][‘children‘]">
<ul>
<foreach name="data[$v2][‘children‘]" item="v3" key="k3">
<li>{$data[$v3][‘menu_name‘]}</li>
<notempty name="data[$v3][‘children‘]">
<ul>
<volist name="data[$v3][‘children‘]" id="v4" key="k4">
<li>{$data[$v4][‘menu_name‘]}</li>
</volist>
</ul>
</notempty>
</foreach>
</ul>
</notempty>
</foreach>
</ul>
</notempty>
</eq>
</foreach>
</ul>
效果:
标签:== empty 无限分类 无限 框架 each list func main
原文地址:https://blog.51cto.com/qicaiji/2479182