在PHP中有Java HashMap吗?

我需要类似于Java中的HashMap的PHP对象,但我没有find当我谷歌search,所以如果有人知道我可以在PHP中模仿HashMaps,帮助将不胜感激。

PHP中的数组可以具有键值结构。

取决于你想要什么,你可能会对SPL对象存储类感兴趣。

http://php.net/manual/en/class.splobjectstorage.php

它可以让你使用对象作为键,有一个接口来计数,获得散列和其他好东西。

 $s = new SplObjectStorage; $o1 = new stdClass; $o2 = new stdClass; $o2->foo = 'bar'; $s[$o1] = 'baz'; $s[$o2] = 'bingo'; echo $s[$o1]; // 'baz' echo $s[$o2]; // 'bingo' 

用O(1)读取复杂度在PHP中创build一个类似HashMap的Java。

打开一个phpshterminal:

 php> $myhashmap = array(); php> $myhashmap['mykey1'] = 'myvalue1'; php> $myhashmap['mykey2'] = 'myvalue2'; php> echo $myhashmap['mykey2']; myvalue2 

在这种情况下, $myhashmap['mykey2']的复杂性似乎是不变的时间O(1),这意味着当$ myhasmap的大小接近无穷大时,获取给定键的值所需的时间保持相同。

certificatePHP数组读取是恒定的时间:

通过PHP解释器运行:

 php> for($x = 0; $x < 1000000000; $x++){ ... $myhashmap[$x] = $x . " derp"; ... } 

循环添加了10亿个键/值,大约需要2分钟将它们全部添加到可能耗尽您的内存的散列表。

然后查看查找需要多长时间:

 php> system('date +%N');echo " " . $myhashmap[10333] . " ";system('date +%N'); 786946389 10333 derp 789008364 

那么PHP数组映射查找速度有多快呢?

10333是我们查找的关键。 1百万纳秒== 1毫秒。 从一个密钥获得一个值需要花费的时间是206万毫微秒或者大约2毫秒。 大约相同的时间,如果数组是空的。 这对我来说看起来是不断的。

 $fruits = array ( "fruits" => array("a" => "Orange", "b" => "Banana", "c" => "Apple"), "numbers" => array(1, 2, 3, 4, 5, 6), "holes" => array("first", 5 => "second", "third") ); echo $fruits["fruits"]["b"] 

输出“香蕉”

取自http://in2.php.net/manual/en/function.array.php

HashMap也适用于除O(1)读取复杂度(取决于您自己的散列函数的质量)的string和整数以外的键。

你可以自己做一个简单的hashMap。 hashMap所做的是使用散列作为索引/键将项目存储在数组中。 散列函数偶尔会发生碰撞(不经常,但可能会发生),所以必须在hashMap中存储多个条目。 这个简单的是一个hashMap:

 class IEqualityComparer { public function equals($x, $y) { throw new Exception("Not implemented!"); } public function getHashCode($obj) { throw new Exception("Not implemented!"); } } class HashMap { private $map = array(); private $comparer; public function __construct(IEqualityComparer $keyComparer) { $this->comparer = $keyComparer; } public function has($key) { $hash = $this->comparer->getHashCode($key); if (!isset($this->map[$hash])) { return false; } foreach ($this->map[$hash] as $item) { if ($this->comparer->equals($item['key'], $key)) { return true; } } return false; } public function get($key) { $hash = $this->comparer->getHashCode($key); if (!isset($this->map[$hash])) { return false; } foreach ($this->map[$hash] as $item) { if ($this->comparer->equals($item['key'], $key)) { return $item['value']; } } return false; } public function del($key) { $hash = $this->comparer->getHashCode($key); if (!isset($this->map[$hash])) { return false; } foreach ($this->map[$hash] as $index => $item) { if ($this->comparer->equals($item['key'], $key)) { unset($this->map[$hash][$index]); if (count($this->map[$hash]) == 0) unset($this->map[$hash]); return true; } } return false; } public function put($key, $value) { $hash = $this->comparer->getHashCode($key); if (!isset($this->map[$hash])) { $this->map[$hash] = array(); } $newItem = array('key' => $key, 'value' => $value); foreach ($this->map[$hash] as $index => $item) { if ($this->comparer->equals($item['key'], $key)) { $this->map[$hash][$index] = $newItem; return; } } $this->map[$hash][] = $newItem; } } 

为了它的function,你还需要一个散列函数为你的密钥和一个比较器的相等(如果你只有一些项目或由于其他原因不需要速度,你可以让散列函数返回0;所有的项目将放在同一个桶里,你会得到O(N)的复杂性)

这里是一个例子:

 class IntArrayComparer extends IEqualityComparer { public function equals($x, $y) { if (count($x) !== count($y)) return false; foreach ($x as $key => $value) { if (!isset($y[$key]) || $y[$key] !== $value) return false; } return true; } public function getHashCode($obj) { $hash = 0; foreach ($obj as $key => $value) $hash ^= $key ^ $value; return $hash; } } $hashmap = new HashMap(new IntArrayComparer()); for ($i = 0; $i < 10; $i++) { for ($j = 0; $j < 10; $j++) { $hashmap->put(array($i, $j), $i * 10 + $j); } } echo $hashmap->get(array(3, 7)) . "<br/>"; echo $hashmap->get(array(5, 1)) . "<br/>"; echo ($hashmap->has(array(8, 4))? 'true': 'false') . "<br/>"; echo ($hashmap->has(array(-1, 9))? 'true': 'false') . "<br/>"; echo ($hashmap->has(array(6))? 'true': 'false') . "<br/>"; echo ($hashmap->has(array(1, 2, 3))? 'true': 'false') . "<br/>"; $hashmap->del(array(8, 4)); echo ($hashmap->has(array(8, 4))? 'true': 'false') . "<br/>"; 

这给出了输出:

 37 51 true false false false false