溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用PHP生成樹

發(fā)布時間:2021-08-04 09:15:18 來源:億速云 閱讀:106 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“怎么用PHP生成樹”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

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

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

運行效果如下圖所示:

怎么用PHP生成樹

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

<?php
class Tree
{
 public $data=array();
 public $cateArray=array();
 public $res=array();
 function Tree()
 {
 }
 function setNode ($id, $parent, $value)
 {
  $parent = $parent?$parent:0;
  $this->data[$id] = $value;
  //print_r($this->data);
  //echo "\r\n";
  $this->cateArray[$id] = $parent; //節(jié)點數(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é)點
 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é)點數(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","臺式機");
$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é)點的所有父節(jié)點
//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."<br>";
 //echo $tree->getLayer($key).$tree->getValue($key)."<br>";
}
*/
$tree->getAll();
foreach($tree->res as $val){
 echo $val."<br>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>生成Tree</title>
</head>
<body>
<h6>生成標(biāo)簽測試</5>
<select>
<?php
 foreach($tree->res as $key=>$val){
  echo "<option value='{$key}'>{$val}</option>";
 }
?>
</select>
</body>
</html>

“怎么用PHP生成樹”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI