PHP生成树的方法


Posted in PHP onJuly 28, 2015

本文实例讲述了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程序设计有所帮助。

PHP 相关文章推荐
PHP配置心得包含MYSQL5乱码解决
Nov 20 PHP
一周学会PHP(视频)Http下载
Dec 12 PHP
在php MYSQL中插入当前时间
Apr 06 PHP
PHP XML操作的各种方法解析(比较详细)
Jun 17 PHP
php下连接mssql2005的代码
Jan 17 PHP
JoshChen_web格式编码UTF8-无BOM的小细节分析
Aug 16 PHP
PHP函数preg_match_all正则表达式的基本使用详细解析
Aug 31 PHP
PHP中通过fopen()函数访问远程文件示例
Nov 18 PHP
php查询mysql数据库并将结果保存到数组的方法
Mar 18 PHP
PHP实现图片自动清理的方法
Jul 08 PHP
php微信公众号开发(2)百度BAE搭建和数据库使用
Dec 15 PHP
iis 7下安装laravel 5.4环境的方法教程
Jun 14 PHP
php计算税后工资的方法
Jul 28 #PHP
怎样搭建PHP开发环境
Jul 28 #PHP
php递归实现无限分类的方法
Jul 28 #PHP
php类自动加载器实现方法
Jul 28 #PHP
PHP实现的json类实例
Jul 28 #PHP
php无序树实现方法
Jul 28 #PHP
分享PHP函数实现数字与文字分页代码
Jul 28 #PHP
You might like
DOTA2 无惧惊涛骇浪 昆卡大型水友攻略
2020/04/20 DOTA
PHP+JQuery+Ajax实现分页方法详解
2016/08/06 PHP
使用ThinkPHP生成缩略图及显示
2017/04/27 PHP
用PHP的反射实现委托模式的讲解
2019/03/22 PHP
php提高脚本性能的4个技巧
2020/08/18 PHP
CL vs ForZe BO5 第四场 2.13
2021/03/10 DOTA
node.js中的fs.lstat方法使用说明
2014/12/16 Javascript
使用JS获取当前地理位置方法汇总
2014/12/18 Javascript
使用jQuery管理选择结果
2015/01/20 Javascript
举例讲解jQuery对DOM元素的向上遍历、向下遍历和水平遍历
2016/07/07 Javascript
前端分页功能的实现以及原理(jQuery)
2017/01/22 Javascript
微信分享调用jssdk实例
2017/06/08 Javascript
基于js的变量提升和函数提升(详解)
2017/09/17 Javascript
vue框架搭建之axios使用教程
2018/07/11 Javascript
vue data恢复初始化数据的实现方法
2019/10/31 Javascript
element el-tree组件的动态加载、新增、更新节点的实现
2020/02/27 Javascript
[02:07]DOTA2超级联赛专访BBC:难忘网吧超神经历
2013/06/09 DOTA
Python的爬虫包Beautiful Soup中用正则表达式来搜索
2016/01/20 Python
Python实现曲线点抽稀算法的示例
2017/10/12 Python
python逆向入门教程
2018/01/15 Python
scrapy-redis的安装部署步骤讲解
2019/02/27 Python
Python3+Pycharm+PyQt5环境搭建步骤图文详解
2019/05/29 Python
Django之提交表单与前后端交互的方法
2019/07/19 Python
Python简易版图书管理系统
2019/08/12 Python
python之MSE、MAE、RMSE的使用
2020/02/24 Python
python实现跨excel sheet复制代码实例
2020/03/03 Python
python 进制转换 int、bin、oct、hex的原理
2021/01/13 Python
CSS3 clip-path 用法介绍详解
2018/03/01 HTML / CSS
公司企业表扬信
2014/01/11 职场文书
道德大讲堂实施方案
2014/05/14 职场文书
暑期培训班招生方案
2014/08/26 职场文书
爱祖国爱家乡演讲稿
2014/09/02 职场文书
大学学生个人总结
2015/02/15 职场文书
单位计划生育责任书
2015/05/09 职场文书
朋友聚会祝酒词
2015/08/10 职场文书
2015年行政管理人员工作总结
2015/10/15 职场文书