亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP生成樹的方法

 更新時間:2015年07月28日 15:26:24   作者:憤怒的土豆  
這篇文章主要介紹了PHP生成樹的方法,實(shí)例分析了php樹與節(jié)點(diǎn)操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下

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

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

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

實(shí)現(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é)點(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."<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>
<h5>生成標(biāo)簽測試</5>
<select>
<?php
 foreach($tree->res as $key=>$val){
  echo "<option value='{$key}'>{$val}</option>";
 }
?>
</select>
</body>
</html>

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

相關(guān)文章

  • PHP正則解析多重循環(huán)模板示例

    PHP正則解析多重循環(huán)模板示例

    這篇文章主要介紹了PHP正則解析多重循環(huán)模板,結(jié)合實(shí)例形式分析了php基于正則的循環(huán)遍歷與解析相關(guān)操作技巧,需要的朋友可以參考下
    2018-06-06
  • ThinkPHP 防止表單重復(fù)提交的方法

    ThinkPHP 防止表單重復(fù)提交的方法

    防止表單重復(fù)提交有很多種方法,其不外乎,客戶端腳本防止刷新,服務(wù)端token驗(yàn)證等等,thinkphp內(nèi)置了表單token驗(yàn)證,可以方便的防止表單重復(fù)提交
    2011-08-08
  • php讀取mysql亂碼,用set names XXX解決的原理分享

    php讀取mysql亂碼,用set names XXX解決的原理分享

    解決亂碼的方法,我們經(jīng)常使用“set names utf8”,那么為什么加上這句代碼就可以解決了呢?下面跟著我一起來深入set names utf8的內(nèi)部執(zhí)行原理
    2011-12-12
  • PHP is_subclass_of函數(shù)的一個BUG和解決方法

    PHP is_subclass_of函數(shù)的一個BUG和解決方法

    這篇文章主要介紹了PHP is_subclass_of函數(shù)的一個BUG和解決方法,這個BUG存在于php5.3.7版本以前,并且針對interface方面,需要的朋友可以參考下
    2014-06-06
  • PHP中的session安全嗎?

    PHP中的session安全嗎?

    PHP中的session安全嗎?PHP只是為我們提供了一個session的實(shí)現(xiàn),后續(xù)的安全工作需要程序員自己靈活去掌握,所以說PHP編程真的很靈活,需要了解PHP中session安全的朋友可以參考一下
    2016-01-01
  • php 基礎(chǔ)函數(shù)

    php 基礎(chǔ)函數(shù)

    本文主要介紹了php的基礎(chǔ)函數(shù)。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • php實(shí)現(xiàn)webservice實(shí)例

    php實(shí)現(xiàn)webservice實(shí)例

    這篇文章主要介紹了php實(shí)現(xiàn)webservice的方法,以實(shí)例形式講述了webservice的原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2014-11-11
  • php實(shí)現(xiàn)的操作excel類詳解

    php實(shí)現(xiàn)的操作excel類詳解

    這篇文章主要介紹了php實(shí)現(xiàn)的操作excel類,較為詳細(xì)的分析說明了PHP操作excel的具體技巧,包括PHP針對excel的創(chuàng)建、打開、讀取、修改等,需要的朋友可以參考下
    2016-01-01
  • 詳解PHP中array_rand函數(shù)的使用方法

    詳解PHP中array_rand函數(shù)的使用方法

    大家都知道array_rand是數(shù)組隨機(jī)函數(shù)了,今天看到一個站長簡單的介紹array_rand性能了,于是把許久沒寫的php再來簡單的看看,我們一起來看看array_rand函數(shù)用法吧。有需要的朋友們可以參考學(xué)習(xí)。
    2016-09-09
  • 在PHP中使用redis

    在PHP中使用redis

    這里是在Mac OS上安裝redis,并在PHP中開啟redis。
    2013-11-11

最新評論