PHP   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP生成树的方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了PHP生成树的方法分享给大家供大家参。具体如下:

这个类不是我写的 只添加了getAll()函数 PHP生成一个树,可以用于产品分类 不知道遍历写的是否优化,如果你有请分享一下吧 -.-!

运行效果如下图所示:

PHP生成树的方法

实现@L_616_12@如下:

PHP;">
data[$id] = $value;
  //print_r($this->data);
  //echo "\r\n";
  $this->cateArray[$id] = $parent; //节点数组
  //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;
 }
 //单线获取父节点
 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
 //获取所有节点数据生成树
 function getAll($id=0,$str="|-"){
  if($id!=0){
   $str=$str."|-";
  }
  //遍历所有数组检查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);
  }
 }
}
//构造树
//setNode ($id,$value)
$tree = new Tree();
$tree->setNode("1","","电器");
$tree->setNode("2","服装");
$tree->setNode("3","1","电脑");
$tree->setNode("4","家电");
$tree->setNode("5","2","男装");
$tree->setNode("6","女装");
$tree->setNode("7","3","笔记本");
$tree->setNode("8","台式机");
$tree->setNode("9","7","惠普");
$tree->setNode("10","戴尔");
$tree->setNode("11","8","火星人");
$tree->setNode("12","5","西装");
$tree->setNode("13","6","上衣");
$tree->setNode("14","9","惠普-N90");
$tree->setNode("15","惠普-N91");
$tree->setNode("16","10","戴尔A11");
$tree->setNode("17","戴尔A12");
$tree->setNode("18","戴尔A13");
$tree->setNode("19","裤子 ");
$tree->setNode("20","13","长袖");
$tree->setNode("21","短袖");
$tree->setNode("22","20","NIKE长袖");
$tree->setNode("23","361长袖");
$tree->setNode("24","22","NIKE长袖-均码");
$tree->setNode("25","NIKE长袖-短码");
$tree->setNode("26","14","惠普-N90-14寸");
$tree->setNode("27","惠普-N90-15寸");
$tree->setNode("28","惠普-N90-17寸");
$tree->setNode("29","28","惠普-N90-17寸-高性能企业版");
$tree->setNode("30","惠普-N90-17寸-普通家用版");
//获取一个节点的所有父节点
//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."
"; } ?> http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> http://www.w3.org/1999/xhtml"> <a href="http://code.js-code.com/tag/shengcheng/" target="_blank" class="keywords">生成</a>Tree
生成标签测试

大佬总结

以上是大佬教程为你收集整理的PHP生成树的方法全部内容,希望文章能够帮你解决PHP生成树的方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:生成树