您好,登錄后才能下訂單哦!
<?php //無限級分類 $arr = array( array('id'=>1,'name'=>'HeNan','parent_id'=>0), array('id'=>2,'name'=>'nanyang','parent_id'=>1), array('id'=>3,'name'=>'tanghe','parent_id'=>2), array('id'=>4,'name'=>'HuBei','parent_id'=>0), array('id'=>5,'name'=>'wuhan','parent_id'=>4), array('id'=>6,'name'=>'FuJian','parent_id'=>0), array('id'=>7,'name'=>'xiamen','parent_id'=>6), array('id'=>8,'name'=>'huangshi','parent_id'=>5) ); //遞歸法找指定欄目的子孫樹 function subtree($data,$id=0,$lev=1){ $arr = array(); foreach($data as $v){ if($v['parent_id']==$id){ $v['lev'] = $lev; $arr[] = $v; $cid = $v['id']; $arr = array_merge($arr,subtree($data,$cid,$lev+1)); } } return $arr; } //遞歸法找指定欄目的家譜樹 function partree($data,$id=0){ $arr = array(); foreach($data as $v){ if($v['id'] == $id){ $arr[] = $v; $cid = $v['parent_id']; $arr = array_merge(partree($data,$cid),$arr); } } return $arr; } //迭代法查找指定欄目的家譜樹 function fpartree($data,$id=0){ $arr = array(); while($id !== 0 ){ foreach($data as $v){ if($v['id'] == $id){ $arr[] = $v; $id = $v['parent_id']; break; } } } return array_reverse($arr); } //排序輸出 $subtree = subtree($arr,0); foreach($subtree as $v){ echo str_repeat('  ',$v['lev']).$v['name'].'<br />'; } ?>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。