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 编写的日历
Oct 09 PHP
一个可以找出源代码中所有中文的工具
Oct 25 PHP
php.ini中的php-5.2.0配置指令详解
Mar 27 PHP
PHP数据库万能引擎类adodb配置使用以及实例集锦
Jun 12 PHP
ThinkPHP框架设计及扩展详解
Nov 25 PHP
php生成随机颜色方法汇总
Dec 03 PHP
php结合curl实现多线程抓取
Jul 09 PHP
PHP版本常用的排序算法汇总
Dec 20 PHP
YII Framework框架教程之缓存用法详解
Mar 14 PHP
浅谈PHP面向对象之访问者模式+组合模式
May 22 PHP
解决Laravel5.5下的toArray问题
Oct 15 PHP
PHP 加密 Password Hashing API基础知识点
Mar 02 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
PHP 的异常处理、错误的抛出及回调函数等面向对象的错误处理方法
2012/12/07 PHP
php简单解析mysqli查询结果的方法(2种方法)
2016/06/29 PHP
php生成mysql的数据字典
2016/07/07 PHP
php实现获取农历(阴历)、节日、节气的类与用法示例
2017/11/20 PHP
基于laravel belongsTo使用详解
2019/10/18 PHP
js wmp操作代码小结(音乐连播功能)
2008/11/08 Javascript
JS获取父节点方法
2009/08/20 Javascript
JavaScript对象创建及继承原理实例解剖
2013/02/28 Javascript
javascript学习笔记整理(概述、变量、数据类型简介)
2015/10/25 Javascript
jquery正则表达式验证(手机号、身份证号、中文名称)
2015/12/31 Javascript
jQuery插件zTree实现删除树子节点的方法示例
2017/03/08 Javascript
vue省市区三联动下拉选择组件的实现
2017/04/28 Javascript
AngularJS 最常用的八种功能(基础知识)
2017/06/26 Javascript
JS库之Three.js 简易入门教程(详解之一)
2017/09/13 Javascript
图片懒加载imgLazyLoading.js使用详解
2020/09/15 Javascript
Vue.js实现可排序的表格组件功能示例
2019/02/19 Javascript
Vue点击切换Class变化,实现Active当前样式操作
2020/07/17 Javascript
浅谈es6中的元编程
2020/12/01 Javascript
实例Python处理XML文件的方法
2015/08/31 Python
Python数据类型之Tuple元组实例详解
2019/05/08 Python
python实现电子书翻页小程序
2019/07/23 Python
在Pytorch中使用样本权重(sample_weight)的正确方法
2019/08/17 Python
Django框架HttpRequest对象用法实例分析
2019/11/01 Python
Python线程障碍对象Barrier原理详解
2019/12/02 Python
Python HTMLTestRunner可视化报告实现过程解析
2020/04/10 Python
基于Tensorflow的MNIST手写数字识别分类
2020/06/17 Python
python实现邮件循环自动发件功能
2020/09/11 Python
python 删除系统中的文件(按时间,大小,扩展名)
2020/11/19 Python
CSS3的一个简单导航栏实现
2015/08/03 HTML / CSS
会计系毕业个人自荐信格式
2013/09/23 职场文书
法律七进实施方案
2014/03/15 职场文书
研究生给导师的自荐信
2015/03/06 职场文书
2015年高中生国庆节演讲稿
2015/07/30 职场文书
学校中层领导培训心得体会
2016/01/11 职场文书
商业计划书如何写?关键问题有哪些?
2019/07/11 职场文书
Moment的feature导致线上bug解决分析
2022/09/23 Javascript