php代码练习《递归》

贡献者:huiben 类别:代码 时间:2016-03-10 10:57:20 收藏数:24 评分:0
返回上页 举报此文章
请选择举报理由:




收藏到我的文章 改错字
<?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;
}
}
声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。
文章热度:
文章难度:
文章质量:
说明:系统根据文章的热度、难度、质量自动认证,已认证的文章将参与打字排名!

本文打字排名TOP20

登录后可见

用户更多文章推荐