How to realize the traversal of infinite tree graph? PHP traversal.
高洛峰2017-05-16 13:17:40
//php输出版本:
public static function toTreeHtml($lists = [])
{
$string.='<ul class="">';
foreach ($lists as $key => $value) {
$string.='<li><input type="checkbox" name="ids[]" />';
$string.=$value['name'];
if (count($value['child'])>0){
$string.=self::toTreeHtml($value['child']);
}
$string.='</li>';
}
$string.='</ul>';
return $string;
}
//js输出版本
function tree(list,ids){
var string='';
string+="<ul class=''>";
for(i in list){
string+="<li class='"+(list[i].pid==0?"item":"")+"'><label><input "+(in_array(list[i].id,ids)?"checked='checked'":"")+" type='checkbox' value='"+list[i].id+"' name='ids[]' />"+list[i].name+"</label>";
if(list[i].child){
string+=tree(list[i].child,ids);
}
string+="</li>";
}
string+="</ul>";
return string;
}