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

超過十多年行業(yè)經(jīng)驗(yàn),技術(shù)領(lǐng)先,服務(wù)至上的經(jīng)營模式,全靠網(wǎng)絡(luò)和口碑獲得客戶,為自己降低成本,也就是為客戶降低成本。到目前業(yè)務(wù)范圍包括了:網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì),成都網(wǎng)站推廣,成都網(wǎng)站優(yōu)化,整體網(wǎng)絡(luò)托管,小程序定制開發(fā),微信開發(fā),app軟件開發(fā)公司,同時(shí)也可以讓客戶的網(wǎng)站和網(wǎng)絡(luò)營銷和我們一樣獲得訂單和生意!
php的框架:1、Laravel,Laravel是一款免費(fèi)并且開源的PHP應(yīng)用框架。2、Phalcon,Phalcon是運(yùn)行速度最快的一個(gè)PHP框架。3、Symfony,Symfony是一款為Web項(xiàng)目準(zhǔn)備的PHP框架。4、Yii,Yii是一款快速、安全和專業(yè)的PHP框架。5、CodeIgniter,CodeIgniter是一款非常敏捷的開源PHP框架。6、CakePHP,CakePHP是一款老牌的PHP框架。7.Kohana,Kohana是一款敏捷但是功能強(qiáng)大的PHP框架。
1.內(nèi)部組成
鍵(key):用于操作數(shù)據(jù)的標(biāo)示,例如PHP數(shù)組中的索引,或者字符串鍵等等。
槽(slot/bucket):哈希表中用于保存數(shù)據(jù)的一個(gè)單元,也就是數(shù)據(jù)真正存放的容器。
哈希函數(shù)(hash function):將key映射(map)到數(shù)據(jù)應(yīng)該存放的slot所在位置的函數(shù)。
2.優(yōu)勢(shì)
通過關(guān)鍵值計(jì)算直接獲取目標(biāo)位置,對(duì)于海量數(shù)據(jù)中的精確查找有非常驚人的速度提升,理論上即使有無限的數(shù)據(jù)量,一個(gè)實(shí)現(xiàn)良好的哈希表依舊可以保持O(1)的查找速度,而O(n)的普通列表此時(shí)已經(jīng)無法正常執(zhí)行查找操作(實(shí)際上不可能,受到JVM可用內(nèi)存限制,機(jī)器內(nèi)存限制等)。
3.應(yīng)用場(chǎng)景
在工程上,經(jīng)常用于通過名稱指定配置信息、通過關(guān)鍵字傳遞參數(shù)、建立對(duì)象與對(duì)象的映射關(guān)系等。目前最流行的NoSql數(shù)據(jù)庫之一redis,整體的使用了哈希表思想。
一言以蔽之,所有使用了鍵值對(duì)的地方,都運(yùn)用到了哈希表思想。
4.使用實(shí)例
<?php
class hashTable
{
private $collection;
private $size = 100;
//初始化哈希表的大小
public function __construct($size='')
{
$bucketsSize = is_int($size)?$size:$this->size;
$this->collection = new SplFixedArray($bucketsSize);
}
//生成散列值,作為存儲(chǔ)數(shù)據(jù)的位置
private function _hashAlgorithm($key)
{
$length = strlen($key);
$hashValue = 0;
for($i=0; $i<$length; $i++) {
$hashValue += ord($key[$i]);
}
return ($hashValue%($this->size));
}
//在相應(yīng)的位置存儲(chǔ)對(duì)應(yīng)的值
public function set($key, $val)
{
$index = $this->_hashAlgorithm($key);
$this->collection[$index] = $val;
}
//根據(jù)鍵生成散列值,進(jìn)而找到對(duì)應(yīng)的值
public function get($key)
{
$index = $this->_hashAlgorithm($key);
return $this->collection[$index];
}
//刪除某個(gè)值,成功返回1,失敗返回0
public function del($key)
{
$index = $this->_hashAlgorithm($key);
if(isset($this->collection[$index])) {
unset($this->collection[$index]);
return 1;
} else {
return 0;
}
}
//判斷某個(gè)值是否存在,存在返回1, 不存在返回0
public function exist($key)
{
$index = $this->_hashAlgorithm($key);
if($this->collection[$index]){
return 1;
} else {
return 0;
}
}
//返回key的個(gè)數(shù)
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:
//按照數(shù)字比較
sort($temp, SORT_NUMERIC);
break;
//按照字符串進(jìn)行比較
case 3:
sort($temp, SORT_STRING);
break;
//根據(jù)本地字符編碼環(huán)境進(jìn)行比較
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:
//按照數(shù)字比較
rsort($temp, SORT_NUMERIC);
break;
//按照字符串進(jìn)行比較
case 3:
rsort($temp, SORT_STRING);
break;
//根據(jù)本地字符編碼環(huán)境進(jìn)行比較
case 4:
rsort($temp, SORT_LOCALE_STRING);
break;
}
echo "<pre>";
print_r($temp);
}
}
//簡(jiǎn)單的測(cè)試
$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);上述內(nèi)容就是如何在php中使用哈希表,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁名稱:如何在php中使用哈希表
URL鏈接:http://www.chinadenli.net/article24/jdgece.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、網(wǎng)站排名、網(wǎng)站維護(hù)、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站制作、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)