成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導(dǎo)航首頁 ? 技術(shù)教程 ? PHP生成樹的方法
全站頭部文字 我要出現(xiàn)在這里
PHP生成樹的方法 768 2023-12-08   

本文實(shí)例講述了PHP生成樹的方法。分享給大家供大家參考。具體如下:

這個類不是我寫的 只添加了getAll()函數(shù)
php生成一個樹,可以用于產(chǎn)品分類
不知道遍歷寫的是否優(yōu)化,如果你有請分享一下吧 -.-!

運(yùn)行效果如下圖所示:

查看圖片

實(shí)現(xiàn)代碼如下:

data[$id] = $value;
  //print_r($this->data);
  //echo "rn";
  $this->cateArray[$id] = $parent; //節(jié)點(diǎn)數(shù)組
  //print_r($this->cateArray);
 }
 function getChildsTree($id=0)
 {
  $childs=array();
  foreach ($this->cateArray as $child=>$parent)
  {
   if ($parent==$id)
   {
    $childs[$child]=$this->getChildsTree($child);
   }
  }
  print_r($childs)."/r/n";
  return $childs;
 }
 function getParentsTree($id=0)
 {
  $parents=array();
  foreach ($this->cateArray as $child=>$parent)
  {
   if ($child ==$id)
   {
    $parents[$parent]=$this->getParentsTree($parent);
   }
  }
  print_r($parents)."/r/n";
  return $parents;
 }
 function getChilds($id=0)
 {
  $childArray=array();
  $childs=$this->getChild($id);
  foreach ($childs as $child)
  {
   $childArray[]=$child;
   $childArray=array_merge($childArray,$this->getChilds($child));
  }
  return $childArray;
 }
 function getChild($id)
 {
  $childs=array();
  foreach ($this->cateArray as $child=>$parent)
  {
  if ($parent==$id)
  {
   $childs[$child]=$child;
  }
  }
  return $childs;
 }
 function getParents($id)
 {
  $parentArray=array();
  $parents=$this->getParent($id);
  foreach ($parents as $parent)
  {
   $parentArray[]=$parent;
   $parentArray=array_merge($parentArray,$this->getParents($parent));
  }
  return $parentArray;
 }
 function getParent($id)
 {
  $parents=array();
  foreach ($this->cateArray as $child=>$parent)
  {
   if ($child==$id)
   {
   $parents[$parent]=$parent;
   }
  }
  return $parents;
 }
 //單線獲取父節(jié)點(diǎn)
 function getNodeLever($id)
 {
  $parents=array();
  if (key_exists($this->cateArray[$id],$this->cateArray))
  {
   $parents[]=$this->cateArray[$id];
   $parents=array_merge($parents,$this->getNodeLever($this->cateArray[$id]));
  }
  return $parents;
 }
 function getLayer($id,$preStr='|-')
 {
  return str_repeat($preStr,count($this->getNodeLever($id)));
 }
 function getValue ($id)
 {
  return $this->data[$id];
 } // end func
 //獲取所有節(jié)點(diǎn)數(shù)據(jù)生成樹
 function getAll($id=0,$str="|-"){
  if($id!=0){
   $str=$str."|-";
  }
  //遍歷所有數(shù)組檢查parent是否有id
  foreach($this->cateArray as $child=>$parent){
   //檢查是否有相等的ID
   if($parent==$id){
    $this->res[$child]=$str.$this->getValue($child);
    $this->getAll($child,$str);
   }
   //$this->res[$child]=$child.$str.$this->getValue($child);
  }
 }
}
//構(gòu)造樹
//setNode ($id, $parent, $value)
$tree = new Tree();
$tree->setNode("1","","電器");
$tree->setNode("2","","服裝");
$tree->setNode("3","1","電腦");
$tree->setNode("4","1","家電");
$tree->setNode("5","2","男裝");
$tree->setNode("6","2","女裝");
$tree->setNode("7","3","筆記本");
$tree->setNode("8","3","臺式機(jī)");
$tree->setNode("9","7","惠普");
$tree->setNode("10","7","戴爾");
$tree->setNode("11","8","火星人");
$tree->setNode("12","5","西裝");
$tree->setNode("13","6","上衣");
$tree->setNode("14","9","惠普-N90");
$tree->setNode("15","9","惠普-N91");
$tree->setNode("16","10","戴爾A11");
$tree->setNode("17","10","戴爾A12");
$tree->setNode("18","10","戴爾A13");
$tree->setNode("19","6","褲子 ");
$tree->setNode("20","13","長袖");
$tree->setNode("21","13","短袖");
$tree->setNode("22","20","NIKE長袖");
$tree->setNode("23","20","361長袖");
$tree->setNode("24","22","NIKE長袖-均碼");
$tree->setNode("25","22","NIKE長袖-短碼");
$tree->setNode("26","14","惠普-N90-14寸");
$tree->setNode("27","14","惠普-N90-15寸");
$tree->setNode("28","14","惠普-N90-17寸");
$tree->setNode("29","28","惠普-N90-17寸-高性能企業(yè)版");
$tree->setNode("30","28","惠普-N90-17寸-普通家用版");
//獲取一個節(jié)點(diǎn)的所有父節(jié)點(diǎn)
//print_r ($tree->getNodeLever(12));
//print_r ($tree->getParentsTree(12));
//$childs = $tree->getChildsTree(1);
//print_r($childs);
//echo "/r/n/r/n/r/n/r/n";
/*
foreach($childs as $key=>$value){
 echo $key."
"; //echo $tree->getLayer($key).$tree->getValue($key)."
"; } */ $tree->getAll(); foreach($tree->res as $val){ echo $val."
"; } ?>
生成標(biāo)簽測試</5>

希望本文所述對大家的php程序設(shè)計有所幫助。


主站蜘蛛池模板: 男同视频在线| 性欲满载| 五年级上册第一单元数学试卷| 地下道的美人鱼| 周秀娜三级大尺度视频| 美丽的坏女人中文字幕| 我的老婆又大肚| 汤唯韩国电影| 难忘的运动会作文| 野兽罪人电影免费观看| 澳门风云3演员表| 晋中电视台| 塔木德全文阅读免费| 木野真琴| 壁纸超清全屏| 山西影视频道| 蒋锐| 美式壁纸| 故乡别来无恙演员表名单| 好一个中国大舞台简谱| 雪豹46集全| 在线麻豆| 虫虫 视频| 山西少儿频道| 真爱复苏| 美女xxx69爽爽免费观妞| 海洋天堂电影免费观看高清| 娟子演的所有电视剧| 塞下曲景然伶儿| 美丽人生在线完整版免费观看| 荒笛子简谱| 补锌之王的食物| 流浪地球海报| 太医派的开胃汤配方| 欠条怎么写才具有法律效力| 第二回合我爱你| 挤黑头挤痘痘视频最猛| 电影《48天》免费观看全集| 情欲狂欢h版| yy直播是正经直播吗| 直播斯诺克直播间|

!!!站長長期在線接!!!

網(wǎng)站、小程序:定制開發(fā)/二次開發(fā)/仿制開發(fā)等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103