欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php實(shí)現(xiàn)的樹形結(jié)構(gòu)數(shù)據(jù)存取類實(shí)例

 更新時(shí)間:2014年11月29日 12:02:40   投稿:shichen2014  
這篇文章主要介紹了php實(shí)現(xiàn)的樹形結(jié)構(gòu)數(shù)據(jù)存取類,實(shí)例演示了以樹形數(shù)據(jù)結(jié)構(gòu)存取數(shù)據(jù)的實(shí)現(xiàn)方法,對(duì)于學(xué)習(xí)基于PHP的數(shù)據(jù)結(jié)構(gòu)有一定的參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了php實(shí)現(xiàn)的樹形結(jié)構(gòu)數(shù)據(jù)存取類。分享給大家供大家參考。

具體實(shí)現(xiàn)代碼如下:

復(fù)制代碼 代碼如下:
<?php
/**
 * Tanphp framework
 *
 *
 * @category   Tanphp
 * @package    Data_structure
 * @version    $Id: Tree.php 25024 2012-11-26 22:22:22 tanbo $
 */
 
/**
 * 樹形結(jié)構(gòu)數(shù)據(jù)存取類
 * 
 * 用于對(duì)樹形結(jié)構(gòu)數(shù)據(jù)進(jìn)行快速的存取
 * 
 * @param array $arr 參數(shù)必須為標(biāo)準(zhǔn)的二維數(shù)組,包含索引字段(id)與表示樹形結(jié)構(gòu)的字段(path),如example中所示
 * 
 * @example <code>
 * $arr = array(
 *  array( 'id' => 1, 'name' => 'php', 'path' => '1' ),
 *  array( 'id' => 3, 'name' => 'php1', 'path' => '1-3' ),
 *  array( 'id' => 2, 'name' => 'mysql', 'path' => '2' ),
 *  array( 'id' => 6, 'name' => 'mysql1', 'path' => '2-6' ),
 *  array( 'id' => 7, 'name' => 'mysql2', 'path' => '2-7' ),
 *  array( 'id' => 5, 'name' => 'php11', 'path' => '1-3-5' ),
 *  array( 'id' => 4, 'name' => 'php2', 'path' => '1-4' ),
 *   );
 *  $cate = new Tree($arr);
 *  
 *  $data = $cate->getChild(2);
 *  
 *  print_r($data->toArray());
 * </code>
 * 
 */
class Tree
{
    public  $_info;                             //節(jié)點(diǎn)信息
    public  $_child = array();                  //子節(jié)點(diǎn)
    private $_parent;                           //父節(jié)點(diǎn)
    private $_data;                             //當(dāng)前操作的臨時(shí)數(shù)據(jù)
    private static $_indexs         = array();  //所有節(jié)點(diǎn)的索引
    private static $_index_key      = 'id';     //索引鍵
    private static $_tree_key       = 'path';   //樹形結(jié)構(gòu)表達(dá)鍵
    private static $_tree_delimiter = '-';      //屬性結(jié)構(gòu)表達(dá)分割符
    
    /**
     * 構(gòu)造函數(shù)
     * 
     * @param array $arr
     * @param boole $force_sort 如果為真,將會(huì)強(qiáng)制對(duì)$arr 進(jìn)行排序
     * @return void
     */
    public function __construct(array $arr = array(),  $force_sort=true)
    {
        if ($force_sort === true) {
            $arr=$this->_array_sort($arr, self::$_tree_key);
        }
        if (!emptyempty($arr)) {
            $this->_init($arr);
        }
    }
    
    /**
     * 初始存儲(chǔ)樹形數(shù)據(jù)
     * 
     * @param array $arr
     * @return void
     */
    private function _init(array $arr)
    {
        foreach ($arr as $item) {
            $path        = $item[self::$_tree_key];
            $paths       = explode(self::$_tree_delimiter, $path);
            $count_paths = count($paths);
            $parent_id   = isset($paths[$count_paths-2]) ? $paths[$count_paths-2] : NULL;
            
            if (   $count_paths>1                                   //如果有父級(jí)
                && array_key_exists($parent_id, self::$_indexs)      //父級(jí)已經(jīng)被存入索引
                && self::$_indexs[$parent_id] instanceof Tree    //父級(jí)為Tree對(duì)象
            ) {
                self::$_indexs[$parent_id]->addChild($item);
            } elseif ($count_paths == 1) {
                $this->addChild($item);
            } else {
                throw new Exception("path數(shù)據(jù)錯(cuò)誤".var_export($item, true));
            }
        }
        
        //print_r(self::$_indexs);
    }
    
    /**
     * 添加子節(jié)點(diǎn)
     * 
     * @param array $item
     * @return void
     */
    public function addChild(array $item, $parent = NULL)
    {
        $child          = new Tree();
        $child->_info   = $item;
        $child->_parent = $parent == NULL ? $this : $parent;
        $child->_parent->_child[] =  $child;
        
        $this->_addIndex($item, $child->_getSelf()); 
    }
    
    /**
     * 添加節(jié)點(diǎn)到索引
     * 
     * @param array $item
     * @param mix $value
     * @return void
     */
    private function _addIndex(array $item, $value)
    {
        if (array_key_exists(self::$_index_key, $item) && is_int($item[self::$_index_key])) {
            self::$_indexs[$item[self::$_index_key]] = $value;
        } else {
            throw new Exception("id字段不存在或者不為字符串");
        }
    }
    
    /**
     * 獲取對(duì)自己的引用
     * 
     * @return Tree object quote
     */
    private function _getSelf()
    {
        return $this;
    }
    
    /**
     * 獲取指定id的節(jié)點(diǎn)的子節(jié)點(diǎn)
     * 
     * @param int $id
     * @return Tree object
     */
    public function getChild($id)
    {
        $data       = self::$_indexs[$id]->_child;
        $this->_data = $data;
        return $this;
    }
    
    /**
     * 獲取指定id的節(jié)點(diǎn)的父節(jié)點(diǎn)
     * 
     * @param int $id
     * @return Tree object
     */
    public function getParent($id)
    {
        $data = self::$_indexs[$id]->_parent;
        $this->_data = $data;
        return $this;
    }
    
    /**
     * 獲取指定id的節(jié)點(diǎn)的同級(jí)節(jié)點(diǎn)
     *
     * @param int $id
     * @return Tree object
     */
    public function getBrother($id)
    {
        $data = self::$_indexs[$id]->_parent->_child;
        $this->_data = $data;
        return $this;
    }
    
    /**
     * 將Tree對(duì)象轉(zhuǎn)化為數(shù)組
     * 
     * @param  object $object
     * @return array
     */
     public function toArray($obj = NULL)
     {
        $obj  = ($obj === NULL) ? $this->_data : $obj;
        $arr  = array();
        $_arr = is_object($obj) ? $this->_getBaseInfo($obj) : $obj;
        
        if (is_array($_arr)) {
            foreach ($_arr as $key => $val){
                
                $val = (is_array($val) || is_object($val)) ? $this->toArray($val) : $val;
                $arr[$key] = $val;
            }
        } else {
            throw new Exception("_arr不是數(shù)組");
        }
     
        return $arr;
    }
    
    /**
     * 過濾_parent等字段,以免造成無限循環(huán)
     * 
     * @param object $obj
     * @return void
     */
    private function _getBaseInfo($obj)
    {
        $vars = get_object_vars($obj);
        $baseInfo['_info']  =  $vars['_info'];
        $baseInfo['_child'] =  $vars['_child'];
        return $baseInfo;
    }
    
    /**
     * 二維數(shù)組排序
     *
     * 根據(jù)指定的鍵名對(duì)二維數(shù)組進(jìn)行升序或者降序排列
     *
     * @param array  $arr 二維數(shù)組
     * @param string $keys
     * @param string $type 必須為 asc或desc
     * @throws 當(dāng)參數(shù)非法時(shí)拋出異常
     * @return 返回排序好的數(shù)組
     */
    private function _array_sort(array $arr, $keys, $type = 'asc') {
        if (!is_string($keys)) {
            throw new Exception("非法參數(shù)keys:參數(shù)keys的類型必須為字符串");
        }
    
        $keysvalue = $new_array = array();
        foreach ($arr as $k=>$v) {
            if (!is_array($v) || !isset($v[$keys])) {
                throw new Exception("參數(shù)arr不是二維數(shù)組或arr子元素中不存在鍵'{$keys}'");
            }
            $keysvalue[$k] = $v[$keys];
        }
    
        switch ($type) {
            case 'asc':
                asort($keysvalue);
                break;
            case 'desc':
                arsort($keysvalue);
                break;
            default:
                throw new Exception("非法參數(shù)type :參數(shù)type的值必須為 'asc' 或 'desc'");
        }
    
        reset($keysvalue);
        foreach ($keysvalue as $k=>$v) {
            $new_array[$k] = $arr[$k];
        }
        return $new_array;
    }
}
?>

希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • php + ajax 實(shí)現(xiàn)的寫入數(shù)據(jù)庫(kù)操作簡(jiǎn)單示例

    php + ajax 實(shí)現(xiàn)的寫入數(shù)據(jù)庫(kù)操作簡(jiǎn)單示例

    這篇文章主要介紹了php + ajax 實(shí)現(xiàn)的寫入數(shù)據(jù)庫(kù)操作,結(jié)合實(shí)例形式分析了php + ajax 寫入數(shù)據(jù)庫(kù)基本原理、操作技巧與相關(guān)使用注意事項(xiàng),需要的朋友可以參考下
    2020-05-05
  • .htaccess文件保護(hù)實(shí)例講解

    .htaccess文件保護(hù)實(shí)例講解

    .htaccess太強(qiáng)大了,但它本身會(huì)不會(huì)被破解掉呢,請(qǐng)問如何保護(hù)它?
    2011-02-02
  • 如何解決PHP無法實(shí)現(xiàn)多線程的問題

    如何解決PHP無法實(shí)現(xiàn)多線程的問題

    有沒有辦法在PHP中實(shí)現(xiàn)多線程呢?其實(shí)的是大多數(shù)情況下,你大可不必使用fork或者線程,并且你會(huì)得到比用fork或thread更好的性能。
    2015-09-09
  • php ignore_user_abort與register_shutdown_function 使用方法

    php ignore_user_abort與register_shutdown_function 使用方法

    php ignore_user_abort與register_shutdown_function 使用方法小結(jié)。
    2009-06-06
  • php進(jìn)程通信之信號(hào)量淺析介紹

    php進(jìn)程通信之信號(hào)量淺析介紹

    信號(hào)量又稱為信號(hào)燈、旗語 用來解決進(jìn)程(線程同步的問題),類似于一把鎖,訪問前獲取鎖(獲取不到則等待),訪問后釋放鎖,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • PHP性能分析工具xhprof的安裝使用與注意事項(xiàng)

    PHP性能分析工具xhprof的安裝使用與注意事項(xiàng)

    xhprof 是一款Facebook工程師開發(fā)和維護(hù)的一款PHP性能分析、調(diào)試工具,相較于xdebug要更輕量,更節(jié)省資源,強(qiáng)烈推薦大家使用。下面這篇文章主要給大家介紹了關(guān)于PHP性能分析工具xhprof的安裝與使用方法的相關(guān)資料,需要的朋友可以參考下。
    2017-12-12
  • php實(shí)現(xiàn)的簡(jiǎn)單檢驗(yàn)登陸類

    php實(shí)現(xiàn)的簡(jiǎn)單檢驗(yàn)登陸類

    這篇文章主要介紹了php實(shí)現(xiàn)的簡(jiǎn)單檢驗(yàn)登陸類,可實(shí)現(xiàn)基本的php數(shù)據(jù)庫(kù)查詢及密碼匹配的功能,需要的朋友可以參考下
    2015-06-06
  • php lcg_value與mt_rand生成0~1隨機(jī)小數(shù)的效果對(duì)比分析

    php lcg_value與mt_rand生成0~1隨機(jī)小數(shù)的效果對(duì)比分析

    下面小編就為大家?guī)硪黄猵hp lcg_value與mt_rand生成0~1隨機(jī)小數(shù)的效果對(duì)比分析。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • php構(gòu)造函數(shù)的繼承方法

    php構(gòu)造函數(shù)的繼承方法

    這篇文章主要介紹了php構(gòu)造函數(shù)的繼承方法,實(shí)例分析并總結(jié)了構(gòu)造函數(shù)繼承的各種常見情況,需要的朋友可以參考下
    2015-02-02
  • PHP 擴(kuò)展Memcached命令用法實(shí)例總結(jié)

    PHP 擴(kuò)展Memcached命令用法實(shí)例總結(jié)

    這篇文章主要介紹了PHP 擴(kuò)展Memcached命令用法,結(jié)合實(shí)例形式總結(jié)分析了PHP 擴(kuò)展Memcached命令基本使用方法與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2020-06-06

最新評(píng)論