最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

php树形结构数据存取实例类

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