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网站在线人数统计
Apr 09 PHP
php下使用以下代码连接并测试
Apr 09 PHP
cmd下运行php脚本
Nov 25 PHP
php中用数组的方法设置cookies
Apr 21 PHP
PHP备份数据库生成SQL文件并下载的函数代码
Feb 05 PHP
解析PHP实现下载文件的两种方法
Jul 05 PHP
ThinkPHP无限级分类原理实现留言与回复功能实例
Oct 31 PHP
微信获取用户地理位置信息的原理与步骤
Nov 12 PHP
PHP分页初探 一个最简单的PHP分页代码的简单实现
Jun 21 PHP
php array_key_exists() 与 isset() 的区别
Oct 24 PHP
PHP简单实现欧拉函数Euler功能示例
Nov 06 PHP
PHP使用观察者模式处理异常信息的方法详解
Sep 24 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
POSIX 风格和兼容 Perl 风格两种正则表达式主要函数的类比(preg_match, preg_replace, ereg, ereg_replace)
2010/10/12 PHP
PHP--用万网的接口实现域名查询功能
2012/12/13 PHP
php利用新浪接口查询ip获取地理位置示例
2014/01/20 PHP
php调用C代码的实现方法
2014/03/11 PHP
php运行提示:Fatal error Allowed memory size内存不足的解决方法
2014/12/17 PHP
PHP代码覆盖率统计详解
2020/07/22 PHP
jquery一句话全选/取消全选
2011/03/01 Javascript
jquery如何获取复选框的值
2013/12/12 Javascript
JavaScript实现文字跟随鼠标特效
2015/08/06 Javascript
jQuery xml字符串的解析、读取及查找方法
2016/03/01 Javascript
AngularJS实现一次监听多个值发生的变化
2016/08/31 Javascript
Angular Module声明和获取重载实例代码
2016/09/14 Javascript
js实现多行文本框统计剩余字数功能
2017/03/28 Javascript
vue.js中Vue-router 2.0基础实践教程
2017/05/08 Javascript
详解 vue better-scroll滚动插件排坑
2018/02/08 Javascript
Vue实现导出excel表格功能
2018/03/30 Javascript
Layer弹出层动态获取数据的方法
2018/08/20 Javascript
vue-cli3脚手架的配置及使用教程
2018/08/28 Javascript
简单分析js中的this的原理
2019/08/31 Javascript
基于JS判断对象是否是数组
2020/01/10 Javascript
解决vue一个页面中复用同一个echarts组件的问题
2020/07/19 Javascript
使用原生javascript开发计算器实例代码
2021/02/21 Javascript
python图片验证码生成代码
2016/07/02 Python
利用Pandas 创建空的DataFrame方法
2018/04/08 Python
python按键按住不放持续响应的实例代码
2019/07/17 Python
python批量处理文件或文件夹
2020/07/28 Python
python 多进程和协程配合使用写入数据
2020/10/30 Python
Django通过设置CORS解决跨域问题
2020/11/26 Python
Gretna Green中文官网:苏格兰格林小镇
2019/10/16 全球购物
运动会广播稿60字
2014/01/15 职场文书
地球物理学专业推荐信
2014/09/08 职场文书
2014领导干部学习焦裕禄同志先进事迹思想汇报
2014/09/19 职场文书
2014年图书馆个人工作总结
2014/12/18 职场文书
2016年教师师德师风承诺书
2016/03/25 职场文书
Mysql使用全文索引(FullText index)的实例代码
2022/04/03 MySQL
聊聊配置 Nginx 访问与错误日志的问题
2022/05/25 Servers