欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP生成树的方法

程序员文章站 2023-12-03 16:40:34
本文实例讲述了php生成树的方法。分享给大家供大家参考。具体如下: 这个类不是我写的 只添加了getall()函数 php生成一个树,可以用于产品分类 不知道遍历...

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

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

运行效果如下图所示:

PHP生成树的方法

实现代码如下:

<?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; //节点数组
  //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, $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寸-高性能企业版");
$tree->setnode("30","28","惠普-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."<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>生成标签测试</5>
<select>
<?php
 foreach($tree->res as $key=>$val){
  echo "<option value='{$key}'>{$val}</option>";
 }
?>
</select>
</body>
</html>

希望本文所述对大家的php程序设计有所帮助。