• 技术文章 >PHP >PHP数组

    哈希表在php中的使用

    小妮浅浅小妮浅浅2021-03-17 10:05:37原创6343

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

    1.内部组成

    (key):用于操作数据的标示,例如PHP数组中的索引,或者字符串键等等。

    (slot/bucket):哈希表中用于保存数据的一个单元,也就是数据真正存放的容器。

    哈希函数(hash function):将key映射(map)到数据应该存放的slot所在位置的函数。

    2.优势

    通过关键值计算直接获取目标位置,对于海量数据中的精确查找有非常惊人的速度提升,理论上即使有的数据量,一个实现良好的哈希表依旧可以保持O(1)的查找速度,而O(n)的普通列表此时已经无法正常执行查找操作(实际上不可能,受到JVM可用内存限制,机器内存限制等)。

    3.应用场景

    在工程上,经常用于通过名称指定配置信息、通过关键字传递参数、建立对象与对象的映射关系等。目前最流行的NoSql数据库之一Redis,整体的使用了哈希表思想。

    一言以蔽之,所有使用了键值对的地方,都运用到了哈希表思想。

    4.使用实例

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    84

    85

    86

    87

    88

    89

    90

    91

    92

    93

    94

    95

    96

    97

    98

    99

    100

    101

    102

    103

    104

    105

    106

    107

    108

    109

    110

    111

    112

    113

    114

    115

    116

    117

    118

    119

    120

    121

    122

    123

    124

    125

    126

    127

    128

    129

    130

    131

    132

    133

    134

    135

    136

    137

    138

    139

    140

    141

    142

    143

    144

    145

    146

    147

    148

    149

    150

    151

    152

    153

    154

    155

    156

    157

    158

    159

    160

    161

    162

    163

    164

    165

    166

    167

    168

    <?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中的使用,大家可以看到在实例代码部分还是很复杂的,所以大家一定要对哈希表的基本组成和概念理解透彻后,才能进行下一步的使用更多php学习指路:php数组

    专题推荐:php 哈希表使用
    上一篇:php中的哈希表是什么 下一篇:php中组合数组的方法

    相关文章推荐

    • php选择排序是什么意思• php插入排序的使用• php快速排序是什么• php查找算法的理解• php数组中的二分查找是什么• php顺序查找的使用• php插值查找的用法• php中的哈希表是什么

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网