国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php樹形結構數據存取實例類

2024-09-04 11:44:01
字體:
來源:轉載
供稿:網友
  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. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 滁州市| 高淳县| 禹州市| 中超| 朝阳县| 杨浦区| 高密市| 镇宁| 余姚市| 东乌珠穆沁旗| 宜都市| 龙口市| 安新县| 宜兴市| 来安县| 体育| 栖霞市| 永胜县| 长寿区| 涡阳县| 沈丘县| 兴化市| 仁布县| 青海省| 固始县| 贡嘎县| 九江县| 旅游| 乐山市| 平南县| 潜江市| 高唐县| 甘德县| 章丘市| 凤台县| 垣曲县| 敖汉旗| 永和县| 靖宇县| 思茅市| 鄱阳县|