溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在php中使用哈希表

發布時間:2021-04-23 15:59:52 來源:億速云 閱讀:137 作者:Leah 欄目:編程語言

本篇文章為大家展示了如何在php中使用哈希表,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

php的框架有哪些

php的框架:1、Laravel,Laravel是一款免費并且開源的PHP應用框架。2、Phalcon,Phalcon是運行速度最快的一個PHP框架。3、Symfony,Symfony是一款為Web項目準備的PHP框架。4、Yii,Yii是一款快速、安全和專業的PHP框架。5、CodeIgniter,CodeIgniter是一款非常敏捷的開源PHP框架。6、CakePHP,CakePHP是一款老牌的PHP框架。7.Kohana,Kohana是一款敏捷但是功能強大的PHP框架。

1.內部組成

鍵(key):用于操作數據的標示,例如PHP數組中的索引,或者字符串鍵等等。

槽(slot/bucket):哈希表中用于保存數據的一個單元,也就是數據真正存放的容器。

哈希函數(hash function):將key映射(map)到數據應該存放的slot所在位置的函數。

2.優勢

通過關鍵值計算直接獲取目標位置,對于海量數據中的精確查找有非常驚人的速度提升,理論上即使有無限的數據量,一個實現良好的哈希表依舊可以保持O(1)的查找速度,而O(n)的普通列表此時已經無法正常執行查找操作(實際上不可能,受到JVM可用內存限制,機器內存限制等)。

3.應用場景

在工程上,經常用于通過名稱指定配置信息、通過關鍵字傳遞參數、建立對象與對象的映射關系等。目前最流行的NoSql數據庫之一Redis,整體的使用了哈希表思想。

一言以蔽之,所有使用了鍵值對的地方,都運用到了哈希表思想。

4.使用實例

<?php
 
class hashTable
{
    private $collection;
    private $size = 100;
 
    //初始化哈希表的大小
    public function __construct($size='')
    {
        $bucketsSize = is_int($size)?$size:$this->size;
        $this->collection = new SplFixedArray($bucketsSize);
    }
 
    //生成散列值,作為存儲數據的位置
    private function _hashAlgorithm($key)
    {
        $length = strlen($key);
        $hashValue = 0;
        for($i=0; $i<$length; $i++) {
            $hashValue += ord($key[$i]);
        }
        return ($hashValue%($this->size));
    }
 
    //在相應的位置存儲對應的值
    public function set($key, $val)
    {
        $index = $this->_hashAlgorithm($key);
        $this->collection[$index] = $val;
    }
 
    //根據鍵生成散列值,進而找到對應的值
    public function get($key)
    {
        $index = $this->_hashAlgorithm($key);
        return $this->collection[$index];
    }
 
    //刪除某個值,成功返回1,失敗返回0
    public function del($key)
    {
        $index = $this->_hashAlgorithm($key);
        if(isset($this->collection[$index])) {
            unset($this->collection[$index]);
            return 1;
        } else {
            return 0;
        }
    }
 
    //判斷某個值是否存在,存在返回1, 不存在返回0
    public function exist($key)
    {
        $index = $this->_hashAlgorithm($key);
        if($this->collection[$index]){
            return 1;
        } else {
            return 0;
        }
    }
 
    //返回key的個數
    public function size()
    {
        $size = 0;
        $length = count($this->collection);
        for($i=0; $i<$length; $i++) {
            if($this->collection[$i]) {
                $size++;
            }
        }
        return $size;
    }
 
    //返回value的序列
    public function val()
    {
        $size = 0;
        $length = count($this->collection);
        for($i=0; $i<$length; $i++) {
            if($this->collection[$i]) {
                echo $this->collection[$i]."<br />";
            }
        }
    }
 
    //排序輸出
    public function sort($type=1)
    {
        $length = count($this->collection);
        $temp = array();
        for($i=0; $i<$length; $i++) {
            if($this->collection[$i]) {
                $temp[] = $this->collection[$i];
            }
        }
 
        switch ($type) {
            case 1:
                //正常比較
                sort($temp, SORT_REGULAR);
                break;
            case 2:
                //按照數字比較
                sort($temp, SORT_NUMERIC);
                break;
            //按照字符串進行比較
            case 3:
                sort($temp, SORT_STRING);
                break;
            //根據本地字符編碼環境進行比較
            case 4:
                sort($temp, SORT_LOCALE_STRING);
                break;
 
        }
        echo "<pre>";
        print_r($temp);
    }
 
    //逆序輸出
    public function rev($type=1)
    {
        $length = count($this->collection);
        $temp = array();
        for($i=0; $i<$length; $i++) {
            if($this->collection[$i]) {
                $temp[] = $this->collection[$i];
            }
        }
 
        switch ($type) {
            case 1:
                //正常比較
                rsort($temp, SORT_REGULAR);
                break;
            case 2:
                //按照數字比較
                rsort($temp, SORT_NUMERIC);
                break;
            //按照字符串進行比較
            case 3:
                rsort($temp, SORT_STRING);
                break;
            //根據本地字符編碼環境進行比較
            case 4:
                rsort($temp, SORT_LOCALE_STRING);
                break;
 
        }
        echo "<pre>";
        print_r($temp);
    }
 
 
}
 
//簡單的測試
$list = new hashTable(200);
$list->set("zero", "zero compare");
$list->set("one", "first test");
$list->set("two", "second test");
$list->set("three", "three test");
$list->set("four", "fouth test");
echo $list->val();
echo "after sorted : <br />";
$list->rev(3);

上述內容就是如何在php中使用哈希表,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

php
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女