PHP生成树的方法

 更新时间:2015年07月28日 15:26:24   作者:愤怒的土豆  
这篇文章主要介绍了PHP生成树的方法,实例分析了php树与节点操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

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

这个类不是我写的 只添加了getAll()函数
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程序设计有所帮助。

相关文章

  • PHP使用curl函数发送Post请求的注意事项

    PHP使用curl函数发送Post请求的注意事项

    这篇文章主要给大家介绍的是PHP使用curl函数发送Post请求的一些注意事项,文中通过示例代码与解释介绍的很详细,对大家学习或则使用PHP具有一定的参考借鉴价值,有需要的朋友们可以跟着小编一起来学习学习吧。
    2016-11-11
  • PHP session反序列化漏洞超详细讲解

    PHP session反序列化漏洞超详细讲解

    这篇文章主要介绍了PHP session反序列化漏洞,php session反序列化漏洞存在的原因是当序列化session和读取反序列化字符时采用的序列化选择器不一样时,处理的方法不一样
    2023-02-02
  • PHP结合Redis+MySQL实现冷热数据交换应用案例详解

    PHP结合Redis+MySQL实现冷热数据交换应用案例详解

    这篇文章主要介绍了PHP结合Redis+MySQL实现冷热数据交换应用案例,结合具体实例形式详细分析了Redis+MySQL冷热数据交换原理、实现方法及相关操作技巧,需要的朋友可以参考下
    2019-07-07
  • php使用json_decode后数字对象转换成了科学计数法的解决方法

    php使用json_decode后数字对象转换成了科学计数法的解决方法

    这篇文章主要介绍了php使用json_decode后数字对象转换成了科学计数法的解决方法,涉及php操作json格式数据与数值转换相关技巧,需要的朋友可以参考下
    2017-02-02
  • 利用php绘制饼状图的实现代码

    利用php绘制饼状图的实现代码

    本篇文章是对使用php绘制饼状图的方法进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • php多进程应用场景实例详解

    php多进程应用场景实例详解

    这篇文章主要介绍了php多进程应用,结合具体实例形式详细分析了php多进程模块依赖pcntl扩展安装、使用相关操作技巧与注意事项,需要的朋友可以参考下
    2019-07-07
  • 通过实例解析PHP数据类型转换方法

    通过实例解析PHP数据类型转换方法

    这篇文章主要介绍了通过实例解析PHP数据类型转换方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • php设计模式之策略模式应用案例详解

    php设计模式之策略模式应用案例详解

    这篇文章主要介绍了php设计模式之策略模式,结合具体应用案例形式详细分析了php策略模式的原理、定义、实现方法及项目应用案例与操作注意事项,需要的朋友可以参考下
    2019-06-06
  • php安装php_rar扩展实现rar文件读取和解压的方法

    php安装php_rar扩展实现rar文件读取和解压的方法

    这篇文章主要介绍了php安装php_rar扩展实现rar文件读取和解压的方法,涉及php扩展组件的安装与使用相关操作技巧,需要的朋友可以参考下
    2016-11-11
  • 学习php设计模式 php实现适配器模式

    学习php设计模式 php实现适配器模式

    这篇文章主要介绍了php设计模式中的适配器模式,使用php实现适配器模式,感兴趣的小伙伴们可以参考一下
    2015-12-12

最新评论