手机版

PHP实现无限分类(无递归)

时间:2021-09-29 来源:互联网 编辑:宝哥软件园 浏览:

无限分类在开发中经常用到,比如部门结构、文章分类。无限分类的难点在于“输出”和“查询”,比如。

将文章分类输出成ul列表形式;在类别a. 1下找到包含在所有类别中的文章。实现原理几种常见的实现方法各有优缺点。“改进的序遍历树”的数据结构便于输出和查询,但在移动分类和常规理解上有些复杂。

2.数据结构。

?Php $ list=array (array ('id'=1,' FID'=0,' title'=' China '),array('id'=2,' fid'=1,' title'='江苏'),array ('id'=3,' FID)。数组(' id'=4,' fid'=8,' title'='江阴'),数组(' id'=5,' fid'=3,' title'='芜湖'),数组(' ID'=6,' FID'=3,' title)。Title'='蚌埠')、array('id'=8,' fid'=8,' title'='无锡');分类通过父类id (fid)分层“连接”以形成分类树。在串联时,值得注意的是,A类的fid不能是其子类的id。

当使用这种数据结构进行输出时,最常用的算法是“递归”。熟悉PHP的朋友一定知道PHP不擅长递归,递归次数有限(100次左右,具体看操作系统和配置)。

由于所有递归都可以通过循环来实现,本文根据PHP语言的特点编写了一套关于“无限级”分类的函数,比递归更高效。

3.输出ul列表表单,将上面的数据输出为下面的HTML。

Ul class=' first-child' div江苏/div ul Li class=' first-child-last-child ' div无锡/div ul Li class=' first-child-last-child ' div江阴/div/Li/ul/Li class=' last-child ' div安徽/div ul li class='first-child'div芜湖/div/Li/Li class=' last-child ' div蚌埠/div /li /ul /li /ul这种HTML结构在前端使用非常方便(使用javas具体实施程序如下:

ul?php echo get_tree_ul($list,1);/ul4。输出选项列表表单。

选择选项值='2 '江苏/选项值='8 '无锡/选项值='4 '江阴/选项值='3 '安徽/选项值='5 '芜湖/选项值=' 6 '合肥/选项值='7 '蚌埠/选项/选择执行如下:

选择?Php //get_tree_option()返回数组,并为每个元素添加一个“深度”列,可以直接输出为$ options=get _ tree _ option ($ list,1);foreach($ options as $ op){ echo ' option value=' '。$op['id']''' .str_repeat(',$op['depth'] * 4)。$op['title']。/option ';}?/select5。找到某个分类的所有子类。

?PHP $ children=get _ tree _ child($ list,0);echo内爆(',',$ children);//输出:1,3,2,7,6,5,8,4?6.查找分类的所有父类。

?PHP $ children=get _ tree _ parent($ list,4);echo内爆(',',$ children);//8, 2, 10?7.相关函数。

?phpffunction get _ tree _ child($ data,$ FID){ $ result=array();$ fids=数组($ FID);do { $ cids=array();$ flag=false foreach($ fids as $ FID){ for($ I=count($ data)-1;$ I=0;$ I-){ $ node=$ data[$ I];if($ node[' FID ']==$ FID){ array _ splice($ data,$i,1);$ result[]=$ node[' id '];$ cids[]=$ node[' id '];$ flag=true } } $ fids=$ cids } while($ flag===true);返回$ result}函数get_tree_parent($data,$ id){ $ result=array();$ obj=array();foreach($ data as $ node){ $ obj[$ node[' id ']]=$ node;} $value=isset($obj[$id])?$ obj[$ id]: null;while($ value){ $ id=null;foreach($ data as $ node){ if($ node[' id ']==$ value[' FID ']){ $ id=$ node[' id '];$ result[]=$ node[' id '];打破;} } if($ id===null){ $ result[]=$ value[' FID '];} $value=isset($obj[$id])?$ obj[$ id]: null;} unset($ obj);返回$ result}函数get_tree_ul($data,$ FID){ $ stack=array($ FID);$ child=array();$ add _ left=array();$ add _ right=array();$ html _ left=array();$ html _ right=array();$ obj=array();$ loop=0;foreach($ data as $ node){ $ PID=$ node[' FID '];if(!isset($ child[$ PID]){ $ child[$ PID]=array();} array_push($child[$pid],$ node[' id ']);$ obj[$ node[' id ']]=$ node;} while(count($ stack)0){ $ id=$ stack[0];$ flag=false $ node=isset($ obj[$ id])?$ obj[$ id]: null;if(isset($ child[$ id]){ $ cids=$ child[$ id];$ length=count($ cids);for($ I=$ length-1;$ I=0;$i - ) { array_unshift($stack,$ cids[$ I]);} $ obj[$ cids[$ length-1]][' IslastChild ']=true;$ obj[$ cids[0]][' IsFirstChild ']=true;$ flag=true} if ($id!=$ fid $节点!isset($ add _ left[$ id]){ if(isset($ node[' isFirstChild '])isset($ node[' isLastChild ']){ $ html _ left[]=' Li class=' first-child-last-child ');} else if(isset($ node[' IsFirstChild ']){ $ html _ left[]=' Li class=' first-child ';} else if(isset($ node[' isLastChild ']){ $ html _ left[]=' Li class=' last-child ');} else { $ html _ left[]=' Li ';} $html_left[]=($flag===true)?div { $ node[' title ']}/div ul ' : ' div { $ node[' title ']}/div ';$ add _ left[$ id]=true;} if ($id!=$ fid $节点!isset($ add _ right[$ id]){ $ html _ right[]=($ flag===true)?/ul/Li ' : '/Li ';$ add _ right[$ id]=true;} if($ flag==false){ if($ node){ $ cids=$ child[$ node[' FID ']];for($ I=count($ cids)-1;$ I=0;$i-){ if($ cids[$ I]==$ id){ array _ splice($ child[$ node[' FID ']],$ I,1);打破;} } if(count($ child[$ node[' FID '])==0){ $ child[$ node[' FID ']=null;} } array_push($html_left,array _ pop($ html _ right));array _ shift($ stack);} $ loopif($loop 5000)返回$ html _ left } unset($ child);unset($ obj);return inquide(',$ html _ left);}函数get_tree_option($data,$ FID){ $ stack=array($ FID);$ child=array();$ add=array();$ options=array();$ obj=array();$ loop=0;$ depth=-1;foreach($ data as $ node){ $ PID=$ node[' FID '];if(!isset($ child[$ PID]){ $ child[$ PID]=array();} array_push($child[$pid],$ node[' id ']);$ obj[$ node[' id ']]=$ node;} while(count($ stack)0){ $ id=$ stack[0];$ flag=false $ node=isset($ obj[$ id])?$ obj[$ id]: null;if(isset($ child[$ id]){ for($ I=count($ child[$ id])-1;$ I=0;$i - ) { array_unshift($stack,$ child[$ id][$ I]);} $ flag=true} if ($id!=$ fid $节点!isset($ add[$ id]){ $ node[' depth ']=$ depth;$ options[]=$ node;$ add[$ id]=true;} if($ flag==true){ $ depth;} else { if($ node){ for($ I=count($ child[$ node[' FID ']])-1;$ I=0;$i-){ if($ child[$ node[' FID ']][$ I]=$ id){ array _ splice($ child[$ node[' FID ']],$ I,1);打破;} } if(count($ child[$ node[' FID '])==0){ $ child[$ node[' FID ']=null;$ depth-;} } array _ shift($ stack);} $ loopif($loop 5000)返回$ options } unset($ child);unset($ obj);返回$ options}?

以上介绍的就是在不使用递归的情况下服务器端编程语言(专业超文本预处理器的缩写)实现无限极分类,希望对大家的学习有所帮助。

版权声明:PHP实现无限分类(无递归)是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。