A simple hash table implementation ....
<?phpclass hashtable{Private $collection; Private $size = 100; Initialize the size of the hash table public function __construct ($size = ") {$bucketsSize = Is_int ($size)? $size: $this->size; $this->collection = new Splfixedarray ($bucketsSize); }//Generate hash value as the location where the data is stored private function _hashalgorithm ($key) {$length = strlen ($key); $hashValue = 0; for ($i =0; $i < $length; $i + +) {$hashValue + ord ($key [$i]); } return ($hashValue% ($this->size)); }//Store the corresponding value in the appropriate location public function set ($key, $val) {$index = $this->_hashalgorithm ($key); $this->collection[$index] = $val; }//Generates a hash value based on the key, and finds the corresponding value public function get ($key) {$index = $this->_hashalgorithm ($key); return $this->collection[$index]; }//Delete a value, successfully returned 1, failed to return 0 public function del ($key) {$index = $this->_hashalgorithm ($key); if (Isset ($this->collection[$index])) {unset ($this->collection[$index]); return 1; } else {return 0; }}//To determine if a value exists, there is a return of 1, there is no return 0 public function exist ($key) {$index = $this->_hashalgorithm ($key); if ($this->collection[$index]) {return 1; } else {return 0; }}//Returns the number of keys public function size () {$size = 0; $length = count ($this->collection); for ($i =0; $i < $length; $i + +) {if ($this->collection[$i]) {$size + +; }} return $size; }//Returns the sequence of value of public function val () {$size = 0; $length = count ($this->collection); for ($i =0; $i < $length; $i + +) {if ($this->collection[$i]) {echo $this->collection[$i]. " <br/> "; }}}//Sort output 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://Normal comparison sort ($temp, sort_regular); Break Case 2://Compare Sort ($temp, sort_numeric) by number; Break Compare by string case 3:sort ($temp, sort_string); Break Compare Case 4:sort ($temp, sort_locale_string) based on the local character encoding environment; Break } echo "<pre>"; Print_r ($temp); }//Reverse output 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://PositiveOften compare Rsort ($temp, sort_regular); Break Case 2://Rsort ($temp, sort_numeric) according to the numbers; Break Compare by string case 3:rsort ($temp, sort_string); Break Compare Case 4:rsort ($temp, sort_locale_string) based on the local character encoding environment; Break } echo "<pre>"; Print_r ($temp); }}//simple Test $list = new hashTable, $list->set ("zero", "zero Compare"), $list->set ("One", "First Test"), $list- >set ("Double", "Second Test"), $list->set ("Three", "three Test"), $list->set ("Four", "Fouth test"), Echo $list- >val (); echo "after sorted: <br/>"; $list->rev (3);
PHP Implementation Hash Table