在PHP和Mysql中实现哈希表的步骤

在PHP和Mysql中实现哈希表的步骤,php,mysql,hashtable,Php,Mysql,Hashtable,我不熟悉编程语言,我正在使用PHP和mysql。我接到一个任务,用php编写哈希表。我需要做的是,存储用户收集的项目,然后显示它。在互联网上做了一些研究之后,我将在实现哈希表时执行以下步骤,如果我错了,请纠正我: 设置表格: ->用户表:uid(int[5])、username(varchar[128])、item_id(int[8]、items_id_hash(int[50]) ->Items表:item_id(int[5])、item_name(varchar[128])、Items_id_

我不熟悉编程语言,我正在使用PHP和mysql。我接到一个任务,用php编写哈希表。我需要做的是,存储用户收集的项目,然后显示它。在互联网上做了一些研究之后,我将在实现哈希表时执行以下步骤,如果我错了,请纠正我:

  • 设置表格:

    ->用户表:uid(int[5])、username(varchar[128])、item_id(int[8]、items_id_hash(int[50])

    ->Items表:item_id(int[5])、item_name(varchar[128])、Items_id_hash(int[50])

  • 创建一个散列函数(如何创建散列函数?我自己创建还是从internet获取?),将一个键转换成散列值,然后插入到数据库中。例如:hash item_id=001到散列值=(例如)12345。然后插入到users表中

  • 显示/搜索。从用户检索哈希值,然后将其与items表进行比较并显示

  • 问题:

  • 我的步骤正确吗
  • 在哪里可以找到好的php哈希函数?我可以使用md5、sha1或salt吗

  • 我认为您关于哈希表的想法有点过时了。哈希表将键分解为相似的列表。例如:哈希表基于名称的第一个字母,因此将有26个列表。您的哈希是名称的第一个字母,这样可以更快地搜索

    md5、sha1用于派生哈希,用于验证数据是否未被篡改。它们通常有128位或160位版本。因此,它需要X数据并通过哈希发送,以生成一个128位字母数字字符串,该字符串无论在何处执行,都应该是相同的。这通常是一个安全问题

    编辑:展开关于如何导出密钥的问题


    您可以利用数据的模数来创建一个用于行的键。在示例数据%X中,X是您希望拥有的键的总数。问题是X很难找到;如果您有20个项,那么将X设置为20是可行的,并且可以快速搜索,因为每个项都有自己的行。但是如果您有10个项00项,则执行%1000是不可行的。执行X=75之类的操作会更好。

    您有两个主要问题:

    1) 要选择的哈希表范例(打开|关闭)哈希表

    2) 哈希表可以是一个带有键索引的简单数组,也可以是冲突情况下的数组引用

    3) 您必须研究哈希键生成算法($hash=ord($string[$i])+($hash 您是指散列值(存储在表中)而不是散列表吗

    我不明白如何以一种有用的方式将数据存储在哈希表中

    要使用MD5创建哈希值,请尝试

    散列('md5','string to hash')

    有关更多详细信息,请参见

    该代码没有使用任何持久数据存储后端,您可以简单地扩展它以添加mysql支持

    您必须在关系数据库中使用一对多关系(bucket、entries)来实现这一点

    思考如何扩展这个基类


    祝你好运

    你的意思是我的哈希表数据应该是这样的:key:axxxx1值:Shirts key:axxxx2值:T恤衫key:dxxx1值:时钟键:dxxx2值:Watch让我们假设你有1000个不同的项目,每个项目都是一个长袖、T恤、时钟或手表,你会为每个T恤衫创建一个列表这些类型(意味着类型是哈希表)。如果项是各种各样的,不能分类怎么办?有没有什么好的建议让键的名称相似?我处于各种各样的项的情况下,很难分类。即使我分类了,每个类别也只有1到5个项。你可以做一些类似于模的事情;所以取数据%X(其中X是您想要拥有的键数),这将成为该行数据所使用的键。查找X的问题是您必须确定好的数字是多少。我的输出是:16数组()
    <?php
    
    /**
            A brief but simple closed hash table class.
            Jorge Niedbalski R. <jnr@niedbalski.org>
    **/
    
    class   HashTable       {
    
            public  $HashTable = array();
            public  $HashTableSize;
    
            public  function __construct($tablesize) 
            {
                    if($tablesize) {
                            $this->HashTableSize = $tablesize;
                    } else {
                            print "Unknown file size\n";
                            return -1;
                    }
            }
    
            public  function __destruct() 
            {
                    unset($this->HashTable);
            }
    
            public  function  generate_bucket($string) 
            {
                    for($i=0; $i <= strlen($string); $i++) {
                            $hash = ord($string[$i]) + ($hash << 5) - $hash;
                    }
                    print "".$this->HashTableSize."\n";
                    return($hash%$this->HashTableSize);
            }
        public  function  add($string, $associated_array)
            {
                      $bucket = $this->generate_bucket($string);
    
                      $tmp_array = array();
                      $tmp_array['string'] = $string;
                      $tmp_array['assoc_array'] = $associated_array;                
    
                      if(!isset($this->HashTable[$bucket])) {
                                    $this->HashTable[$bucket] = $tmp_array;
                      } else {
                            if(is_array($this->HashTable[$bucket])) {
                                    array_push($this->HashTable[$bucket], $tmp_array);
                            } else {
                                    $tmp = $this->HashTable[$bucket];
                                    $this->HashTable[$bucket] = array();
                                    array_push($this->HashTable[$bucket], $tmp);
                                    array_push($this->HashTable[$bucket], $tmp_array);
                            }
                    }
    
            }
    
            public  function  delete($string, $attrname, $attrvalue) 
            {       
                    $bucket = $this->generate_bucket($string);
    
                    if(is_null($this->HashTable[$bucket])) {
                                    return -1;
                    } else {
                            if(is_array($this->HashTable[$bucket])) {
                                    for($x = 0; $x <= sizeof($this->HashTable[$bucket]); $x++) {
                                            if(($this->HashTable[$bucket][$x]['string'] == $string) && ($this->HashTable[$bucket][$x]['.$attrname.'] == $attrvalue)) {
                                                    unset($this->HashTable[$bucket][$x]);   
                                            }
                                    }
        } else {
                                    unset($this->HashTable[$bucket][$x]);
                            }
                    }       
                    /** everything is OK **/                        
                    return 0;
            }
    
    
            public  function  search($string) 
            {
                    $resultArray = array();
    
                    $bucket = $this->generate_bucket($string);
    
                    if(is_null($this->HashTable[$bucket])) {
                            return -1;
                    } else {
                            if(is_array($this->HashTable[$bucket])) {
                                    for($x = 0; $x <= sizeof($this->HashTable[$bucket]); $x++) {
                                            if(strcmp($this->HashTable[$bucket][$x]['string'], $string) == 0) {
                                                    array_push($resultArray,$this->HashTable[$bucket][$x]);
                                            }
                                     }
                            } else {
                                    array_push($resultArray,$this->HashTable[$bucket]);
                            }
                    }
    
                    return($resultArray);
            }
    }
    
            $hash = new HashTable(16);
    
            $arr = array('nombre' => "jorge niedbalski");
    
            $hash->add("astroza", $arr);
            $hash->add("astrozas", $arr);
    
            print_r($hash->search("astroza"));
    
    ?>