VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • php实现的树形结构数据存取类实例

这篇文章主要介绍了php实现的树形结构数据存取类,实例演示了以树形数据结构存取数据的实现方法,对于学习基于PHP的数据结构有一定的参考借鉴价值,需要的朋友可以参考下

本文实例讲述了php实现的树形结构数据存取类。分享给大家供大家参考。

具体实现代码如下:

  1. <?php 
  2. /** 
  3.  * Tanphp framework 
  4.  * 
  5.  * 
  6.  * @category   Tanphp 
  7.  * @package    Data_structure 
  8.  * @version    $Id: Tree.php 25024 2012-11-26 22:22:22 tanbo $ 
  9.  */ 
  10.  
  11. /** 
  12.  * 树形结构数据存取类 
  13.  *  
  14.  * 用于对树形结构数据进行快速的存取 
  15.  *  
  16.  * @param array $arr 参数必须为标准的二维数组,包含索引字段(id)与表示树形结构的字段(path),如example中所示 
  17.  *  
  18.  * @example <code> 
  19.  * $arr = array( 
  20.  *  array( 'id' => 1, 'name' => 'php', 'path' => '1' ), 
  21.  *  array( 'id' => 3, 'name' => 'php1', 'path' => '1-3' ), 
  22.  *  array( 'id' => 2, 'name' => 'mysql', 'path' => '2' ), 
  23.  *  array( 'id' => 6, 'name' => 'mysql1', 'path' => '2-6' ), 
  24.  *  array( 'id' => 7, 'name' => 'mysql2', 'path' => '2-7' ), 
  25.  *  array( 'id' => 5, 'name' => 'php11', 'path' => '1-3-5' ), 
  26.  *  array( 'id' => 4, 'name' => 'php2', 'path' => '1-4' ), 
  27.  *   ); 
  28.  *  $cate = new Tree($arr); 
  29.  *   
  30.  *  $data = $cate->getChild(2); 
  31.  *   
  32.  *  print_r($data->toArray()); 
  33.  * </code> 
  34.  *  
  35.  */ 
  36. class Tree 
  37. { 
  38.     public  $_info;                             //节点信息 
  39.     public  $_child = array();                  //子节点 
  40.     private $_parent;                           //父节点 
  41.     private $_data;                             //当前操作的临时数据 
  42.     private static $_indexs         = array();  //所有节点的索引 
  43.     private static $_index_key      = 'id';     //索引键 
  44.     private static $_tree_key       = 'path';   //树形结构表达键 
  45.     private static $_tree_delimiter = '-';      //属性结构表达分割符 
  46.      
  47.     /** 
  48.      * 构造函数 
  49.      *  
  50.      * @param array $arr 
  51.      * @param boole $force_sort 如果为真,将会强制对$arr 进行排序 
  52.      * @return void 
  53.      */ 
  54.     public function __construct(array $arr = array(),  $force_sort=true) 
  55.     { 
  56.         if ($force_sort === true) { 
  57.             $arr=$this->_array_sort($arr, self::$_tree_key); 
  58.         } 
  59.         if (!emptyempty($arr)) { 
  60.             $this->_init($arr); 
  61.         } 
  62.     } 
  63.      
  64.     /** 
  65.      * 初始存储树形数据 
  66.      *  
  67.      * @param array $arr 
  68.      * @return void 
  69.      */ 
  70.     private function _init(array $arr) 
  71.     { 
  72.         foreach ($arr as $item) { 
  73.             $path        = $item[self::$_tree_key]; 
  74.             $paths       = explode(self::$_tree_delimiter, $path); 
  75.             $count_paths = count($paths); 
  76.             $parent_id   = isset($paths[$count_paths-2]) ? $paths[$count_paths-2] : NULL; 
  77.              
  78.             if (   $count_paths>1                                   //如果有父级 
  79.                 && array_key_exists($parent_id, self::$_indexs)      //父级已经被存入索引 
  80.                 && self::$_indexs[$parent_id] instanceof Tree    //父级为Tree对象 
  81.             ) { 
  82.                 self::$_indexs[$parent_id]->addChild($item); 
  83.             } elseif ($count_paths == 1) { 
  84.                 $this->addChild($item); 
  85.             } else { 
  86.                 throw new Exception("path数据错误".var_export($item, true)); 
  87.             } 
  88.         } 
  89.          
  90.         //print_r(self::$_indexs); 
  91.     } 
  92.      
  93.     /** 
  94.      * 添加子节点 
  95.      *  
  96.      * @param array $item 
  97.      * @return void 
  98.      */ 
  99.     public function addChild(array $item, $parent = NULL) 
  100.     { 
  101.         $child          = new Tree(); 
  102.         $child->_info   = $item; 
  103.         $child->_parent = $parent == NULL ? $this : $parent; 
  104.         $child->_parent->_child[] =  $child; 
  105.          
  106.         $this->_addIndex($item, $child->_getSelf());  
  107.     } 
  108.      
  109.     /** 
  110.      * 添加节点到索引 
  111.      *  
  112.      * @param array $item 
  113.      * @param mix $value 
  114.      * @return void 
  115.      */ 
  116.     private function _addIndex(array $item, $value) 
  117.     { 
  118.         if (array_key_exists(self::$_index_key, $item) && is_int($item[self::$_index_key])) { 
  119.             self::$_indexs[$item[self::$_index_key]] = $value; 
  120.         } else { 
  121.             throw new Exception("id字段不存在或者不为字符串"); 
  122.         } 
  123.     } 
  124.      
  125.     /** 
  126.      * 获取对自己的引用 
  127.      *  
  128.      * @return Tree object quote 
  129.      */ 
  130.     private function _getSelf() 
  131.     { 
  132.         return $this; 
  133.     } 
  134.      
  135.     /** 
  136.      * 获取指定id的节点的子节点 
  137.      *  
  138.      * @param int $id 
  139.      * @return Tree object 
  140.      */ 
  141.     public function getChild($id) 
  142.     { 
  143.         $data       = self::$_indexs[$id]->_child; 
  144.         $this->_data = $data; 
  145.         return $this; 
  146.     } 
  147.      
  148.     /** 
  149.      * 获取指定id的节点的父节点 
  150.      *  
  151.      * @param int $id 
  152.      * @return Tree object 
  153.      */ 
  154.     public function getParent($id) 
  155.     { 
  156.         $data = self::$_indexs[$id]->_parent; 
  157.         $this->_data = $data; 
  158.         return $this; 
  159.     } 
  160.      
  161.     /** 
  162.      * 获取指定id的节点的同级节点 
  163.      * 
  164.      * @param int $id 
  165.      * @return Tree object 
  166.      */ 
  167.     public function getBrother($id) 
  168.     { 
  169.         $data = self::$_indexs[$id]->_parent->_child; 
  170.         $this->_data = $data; 
  171.         return $this; 
  172.     } 
  173.      
  174.     /** 
  175.      * 将Tree对象转化为数组 
  176.      *  
  177.      * @param  object $object 
  178.      * @return array 
  179.      */ 
  180.      public function toArray($obj = NULL) 
  181.      { 
  182.         $obj  = ($obj === NULL) ? $this->_data : $obj; 
  183.         $arr  = array(); 
  184.         $_arr = is_object($obj) ? $this->_getBaseInfo($obj) : $obj; 
  185.          
  186.         if (is_array($_arr)) { 
  187.             foreach ($_arr as $key => $val){ 
  188.                  
  189.                 $val = (is_array($val) || is_object($val)) ? $this->toArray($val) : $val; 
  190.                 $arr[$key] = $val; 
  191.             } 
  192.         } else { 
  193.             throw new Exception("_arr不是数组"); 
  194.         } 
  195.       
  196.         return $arr; 
  197.     } 
  198.      
  199.     /** 
  200.      * 过滤_parent等字段,以免造成无限循环 
  201.      *  
  202.      * @param object $obj 
  203.      * @return void 
  204.      */ 
  205.     private function _getBaseInfo($obj) 
  206.     { 
  207.         $vars = get_object_vars($obj); 
  208.         $baseInfo['_info']  =  $vars['_info']; 
  209.         $baseInfo['_child'] =  $vars['_child']; 
  210.         return $baseInfo; 
  211.     } 
  212.      
  213.     /** 
  214.      * 二维数组排序 
  215.      * 
  216.      * 根据指定的键名对二维数组进行升序或者降序排列 
  217.      * 
  218.      * @param array  $arr 二维数组 
  219.      * @param string $keys 
  220.      * @param string $type 必须为 asc或desc 
  221.      * @throws 当参数非法时抛出异常 
  222.      * @return 返回排序好的数组 
  223.      */ 
  224.     private function _array_sort(array $arr, $keys, $type = 'asc') { 
  225.         if (!is_string($keys)) { 
  226.             throw new Exception("非法参数keys:参数keys的类型必须为字符串"); 
  227.         } 
  228.      
  229.         $keysvalue = $new_array = array(); 
  230.         foreach ($arr as $k=>$v) { 
  231.             if (!is_array($v) || !isset($v[$keys])) { 
  232.                 throw new Exception("参数arr不是二维数组或arr子元素中不存在键'{$keys}'"); 
  233.             } 
  234.             $keysvalue[$k] = $v[$keys]; 
  235.         } 
  236.      
  237.         switch ($type) { 
  238.             case 'asc': 
  239.                 asort($keysvalue); 
  240.                 break; 
  241.             case 'desc': 
  242.                 arsort($keysvalue); 
  243.                 break; 
  244.             default: 
  245.                 throw new Exception("非法参数type :参数type的值必须为 'asc' 或 'desc'"); 
  246.         } 
  247.      
  248.         reset($keysvalue); 
  249.         foreach ($keysvalue as $k=>$v) { 
  250.             $new_array[$k] = $arr[$k]; 
  251.         } 
  252.         return $new_array; 
  253.     } 
  254. } 
  255. ?> 
希望本文所述对大家的PHP程序设计有所帮助。
 



出处:http://www.phpfensi.com/php/20210501/14725.html


相关教程