- <?php
- class TestControl extends Control{
- private $temp = array(
- array("id"=>3,"name"=>"小说","pid"=>1),
- array("id"=>4,"name"=>"足球","pid"=>2),
- array("id"=>1,"name"=>"书","pid"=>0),
- array("id"=>2,"name"=>"球","pid"=>0),
- array("id"=>5,"name"=>"陶瓷","pid"=>0),
- array("id"=>6,"name"=>"言情小说","pid"=>3)
- );
- public function __construct(){
- parent::__construct();
- }
- public function recursion(){
- $ret = $this->recursionArray($this->temp,0);
- $html = $this->cHtml($ret);
- echo $html;
- }
- public function recursionArray($arr,$pid){
- $tree = '';
- foreach ($arr as $key=>$value){
- if($value['pid'] == $pid){
- $value['pid'] = $this->recursionArray($arr,$value['id']);
- $tree[] = $value;
- }
- }
- return $tree;
- }
- public function cHtml($arr){
- // $html = "";
- // foreach ($arr as $key=>$value){
- // if($value['pid'] == $pid){
- // $html .= "<ul>";
- // $html .= "<li class=''>".$value['name'];
- // $html .= $this->cHtml($arr,$value['id']);
- // $html .= "</li>";
- // $html .= "</ul>";
- // }
- // }
- // return $html;
- $html = "";
- $html .= "<ul>";
- foreach ($arr as $key=>$value){
- if(is_array($value['pid'])){
- $html .="<li class='main'>".$value['name'];
- $html .= $this->cHtml($value['pid']);
- }else {
- $html .="<li class=''>".$value['name'];
- }
- $html .= "</li>";
- }
- $html .= "</ul>";
- return $html;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2405201614694.html
来源: http://www.codesnippet.cn/detail/2405201614694.html