php专区

 首页 > php专区 > PHP应用 > php类库 > php树形结构数据存取实例类 - php类库

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

分享到:
【字体:
导读:
          ?php ***Tanphpframework***@categoryTanphp*@packageData_structure*@copyrightCopyright(c)2012谭博tanbo name*@version$Id:Tree php250...

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

  1. /** 
  2.  * Tanphp framework 
  3.  * 
  4.  * 
  5.  * @category   Tanphp 
  6.  * @package    Data_structure 
  7.  * @copyright  Copyright (c) 2012 谭博  tanbo.name 
  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  
  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.  *  
  34.  *  
  35.  */ 
  36. class Tree 
  37.     public  $_info;                             //节点信息 
  38.     public  $_child = array();                  //子节点 
  39.     private $_parent;                           //父节点 
  40.     private $_data;                             //当前操作的临时数据 
  41.     private static $_indexs         = array();  //所有节点的索引 
  42.     private static $_index_key      = 'id';     //索引键 
  43.     private static $_tree_key       = 'path';   //树形结构表达键 
  44.     private static $_tree_delimiter = '-';      //属性结构表达分割符 
  45.      
  46.      
  47.      
  48.     /** 
  49.      * 构造函数 
  50.      *  
  51.      * @param array $arr 
  52.      * @param boole $force_sort 如果为真,将会强制对$arr 进行排序 
  53.      * @return void 
  54.      */ 
  55.     public function __construct(array $arr = array(),  $force_sort=true) 
  56.     { 
  57.         if ($force_sort === true) { 
  58.             $arr=$this->_array_sort($arr, self::$_tree_key); 
  59.         } 
  60.         if (!emptyempty($arr)) { 
  61.             $this->_init($arr); 
  62.         } 
  63.     } 
  64.      
  65.     /** 
  66.      * 初始存储树形数据 
  67.      *  
  68.      * @param array $arr 
  69.      * @return void 
  70.      */ 
  71.     private function _init(array $arr
  72.     { 
  73.         foreach ($arr as $item) { 
  74.             $path        = $item[self::$_tree_key]; 
  75.             $paths       = explode(self::$_tree_delimiter$path); 
  76.             $count_paths = count($paths); 
  77.             $parent_id   = isset($paths[$count_paths-2]) ? $paths[$count_paths-2] : NULL; 
  78.              
  79.             if (   $count_paths>1                                   //如果有父级 
  80.                 && array_key_exists($parent_id, self::$_indexs)      //父级已经被存入索引 
  81.                 && self::$_indexs[$parent_id] instanceof Tree    //父级为Tree对象 
  82.             ) { 
  83.                 self::$_indexs[$parent_id]->addChild($item); 
  84.             } elseif ($count_paths == 1) { 
  85.                 $this->addChild($item); 
  86.             } else { 
  87.                 throw new Exception("path数据错误".var_export($item, true)); 
  88.             } 
  89.              
  90.         } 
  91.          
  92.         //print_r(self::$_indexs); 
  93.     } 
  94.      
  95.     /** 
  96.      * 添加子节点 
  97.      *  
  98.      * @param array $item 
  99.      * @return void 
  100.      */ 
  101.     public function addChild(array $item$parent = NULL) 
  102.     { 
  103.         $child          = new Tree(); 
  104.         $child->_info   = $item
  105.         $child->_parent = $parent == NULL ? $this : $parent
  106.         $child->_parent->_child[] =  $child
  107.          
  108.         $this->_addIndex($item$child->_getSelf());  
  109.     } 
  110.      
  111.     /** 
  112.      * 添加节点到索引 
  113.      *  
  114.      * @param array $item 
  115.      * @param mix $value 
  116.      * @return void 
  117.      */ 
  118.     private function _addIndex(array $item$value
  119.     { 
  120.         if (array_key_exists(self::$_index_key$item) && is_int($item[self::$_index_key])) { 
  121.             self::$_indexs[$item[self::$_index_key]] = $value
  122.         } else { 
  123.             throw new Exception("id字段不存在或者不为字符串"); 
  124.         } 
  125.     } 
  126.      
  127.      
  128.     /** 
  129.      * 获取对自己的引用 
  130.      *  
  131.      * @return Tree object quote 
  132.      */ 
  133.     private function _getSelf() 
  134.     { 
  135.         return $this
  136.     } 
  137.      
  138.     /** 
  139.      * 获取指定id的节点的子节点 
  140.      *  
  141.      * @param int $id 
  142.      * @return Tree object 
  143.      */ 
  144.     public function getChild($id
  145.     { 
  146.         $data       = self::$_indexs[$id]->_child; 
  147.         $this->_data = $data
  148.         return $this
  149.     } 
  150.      
  151.     /** 
  152.      * 获取指定id的节点的父节点 
  153.      *  
  154.      * @param int $id 
  155.      * @return Tree object 
  156.      */ 
  157.     public function getParent($id
  158.     { 
  159.         $data = self::$_indexs[$id]->_parent; 
  160.         $this->_data = $data
  161.         return $this
  162.     } 
  163.      
  164.     /** 
  165.      * 获取指定id的节点的同级节点 
  166.      * 
  167.      * @param int $id 
  168.      * @return Tree object 
  169.      */ 
  170.     public function getBrother($id
  171.     { 
  172.         $data = self::$_indexs[$id]->_parent->_child; 
  173.         $this->_data = $data
  174.         return $this
  175.     } 
  176.      
  177.     /** 
  178.      * 将Tree对象转化为数组 
  179.      *  
  180.      * @param  object $object 
  181.      * @return array 
  182.      */ 
  183.      public function toArray($obj = NULL) 
  184.      { 
  185.         $obj  = ($obj === NULL) ? $this->_data : $obj
  186.         $arr  = array(); 
  187.         $_arr = is_object($obj) ? $this->_getBaseInfo($obj) : $obj
  188.          
  189.         if (is_array($_arr)) { 
  190.             foreach ($_arr as $key => $val){ 
  191.                  
  192.                 $val = (is_array($val) || is_object($val)) ? $this->toArray($val) : $val
  193.                      
  194.                 $arr[$key] = $val
  195.             } 
  196.         } else { 
  197.             throw new Exception("_arr不是数组"); 
  198.         } 
  199.          
  200.       
  201.         return $arr
  202.     } 
  203.      
  204.     /** 
  205.      * 过滤_parent等字段,以免造成无限循环 
  206.      *  
  207.      * @param object $obj 
  208.      * @return void 
  209.      */ 
  210.     private function _getBaseInfo($obj
  211.     { 
  212.         $vars = get_object_vars($obj); 
  213.         $baseInfo['_info']  =  $vars['_info']; 
  214.         $baseInfo['_child'] =  $vars['_child']; 
  215.         return $baseInfo
  216.     } 
  217.      
  218.     /** 
  219.      * 二维数组排序 
  220.      * 
  221.      * 根据指定的键名对二维数组进行升序或者降序排列 
  222.      * 
  223.      * @param array  $arr 二维数组 
  224.      * @param string $keys 
  225.      * @param string $type 必须为 asc或desc 
  226.      * @throws 当参数非法时抛出异常 
  227.      * @return 返回排序好的数组 
  228.      */ 
  229.     private function _array_sort(array $arr$keys$type = 'asc') { 
  230.         if (!is_string($keys)) { 
  231.             throw new Exception("非法参数keys:参数keys的类型必须为字符串"); 
  232.         } 
  233.      
  234.         $keysvalue = $new_array = array(); 
  235.         foreach ($arr as $k=>$v) { 
  236.             if (!is_array($v) || !isset($v[$keys])) { 
  237.                 throw new Exception("参数arr不是二维数组或arr子元素中不存在键'{$keys}'"); 
  238.             } 
  239.             $keysvalue[$k] = $v[$keys]; 
  240.         } 
  241.      
  242.         switch ($type) { 
  243.             case 'asc'
  244.                 asort($keysvalue); 
  245.                 break
  246.             case 'desc'
  247.                 arsort($keysvalue); 
  248.                 break
  249.             default
  250.                 throw new Exception("非法参数type :参数type的值必须为 'asc' 或 'desc'"); 
  251.         } 
  252.      
  253.         reset($keysvalue); 
  254.         foreach ($keysvalue as $k=>$v) { 
  255.             $new_array[$k] = $arr[$k]; 
  256.         } 
  257.         return $new_array
  258.     } 
  259.      
  260. ?> 
分享到:
PHP货币换算程序代码 - php类库
PHP货币换算程序代码
php版微信公众平台账号自定义菜单类 - ph...
php版微信公众平台账号自定义菜单类 微信公众平台服务号可申请自定义菜单了,其它的号暂时不支持自定义菜单了,这个不但可以使用api来操作,还可以直接在后台定义菜单与参数哦。 申请自定义菜单 服务号可以申请自定义菜单,使用QQ登录的公众号,可以升级为邮箱登录,使用邮箱登录的公众号,可以修...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……