作者:yanbin0220_252 | 来源:互联网 | 2018-03-24 16:11
php代码
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 .= "
";
// $html .= "- ".$value['name'];
// $html .= $this->cHtml($arr,$value['id']);
// $html .= "
";
// $html .= "
";
// }
// }
// return $html;
$html = "";
$html .= "
";
foreach ($arr as $key=>$value){
if(is_array($value['pid'])){
$html .="- ".$value['name'];
$html .= $this->cHtml($value['pid']);
}else {
$html .="
- ".$value['name'];
}
$html .= "
";
}
$html .= "
";
return $html;
}
}