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

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

出处:http://www.phpfensi.com/php/20140107/1139.html


相关教程