温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php中哈希表指的是什么

发布时间:2021-04-30 13:49:27 来源:亿速云 阅读:79 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关php中哈希表指的是什么的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

php有什么特点

1、执行速度快。2、具有很好的开放性和可扩展性。3、PHP支持多种主流与非主流的数据库。4、面向对象编程:PHP提供了类和对象。5、版本更新速度快。6、具有丰富的功能。7、可伸缩性。8、功能全面,包括图形处理、编码与解码、压缩文件处理、xml解析等。

本文操作系统:windows7系统、PHP5.6版本、DELL G3电脑。

1.概念

哈希表是一种通过哈希函数,将特定的键映射到特定值的一种数据结构,它维护键和值之间一一对应关系。

2.说明

(1)哈希表是一种数据结构

(2)哈希表表示了关键码值和记录的映射关系

(3)哈希表可以加快查找速度

(4)任意哈希表,都满足有哈希函数f(key),代入任意key值都可以获取包含该key值的记录在表中的地址

3.实例

<?php
 
class HashTable
{
private $buckets;   //用于存储数据的数组
private $size = 12;   //记录buckets 数组的大小
public function __construct(){
$this->buckets = new SplFixedArray($this->size);
//SplFixedArray效率更高,也可以用一般的数组来代替
}
 
    private function hashfunc($key){
$strlen = strlen($key); //返回字符串的长度
$hashval = 0;  
for($i = 0; $i<$strlen ; $i++){
$hashval +=ord($key[$i]); //返回ASCII的值
}
return $hashval%12;    //    返回取余数后的值
}
public function insert($key,$value){
$index = $this->hashfunc($key);
if(isset($this->buckets[$index])){
$newNode = new HashNode($key,$value,$this->buckets[$index]);
}else{
$newNode = new HashNode($key,$value,null);
}
$this->buckets[$index] = $newNode;
}
public function find($key){
$index = $this->hashfunc($key);
$current = $this->buckets[$index];
echo "</br>";
var_dump($current);
while(isset($current)){    //遍历当前链表
if($current->key==$key){    //比较当前结点关键字
return $current->value;
}
$current = $current->nextNode;
//return $current->value;
}
return NULL;
}
}
 class HashNode{
public $key;  //关键字
public $value;  //数据
public $nextNode; //HASHNODE来存储信息
public function __construct($key,$value,$nextNode = NULL){
$this->key = $key;
$this->value = $value;
$this->nextNode = $nextNode;
}
}
  $ht = new HashTable();
  $ht->insert('Bucket1','value1');
  $ht->insert('Bucket2','value2');
  $ht->insert('Bucket3','value3');
  echo $ht->find('Bucket1');
?>

感谢各位的阅读!关于“php中哈希表指的是什么”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI