The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.
Map() constructorMap::clear()Map::delete()Map::entries()Map::forEach()Map::get()Map::has()Map::keys()Map::set()Map::values()Map::sizeCreates a new Map object.
// array
$myMap = new Map(['foo', 'bar']);
// null
$myMap = new Map(null);
// object
$myMap = new Map(new \stdClass());
// string
$myMap = new Map('one');
The clear() method removes all elements from map.
$myMap = new Map(['foo' => 'bar']);
$myMap->size;
// 1
$myMap->clear();
$myMap->size;
// 0
The delete() method removes a specified element from map.
$myMap = new Map(['foo' => 'bar']);
$myMap->delete('foo');
// true
$myMap->delete(1);
// false
The entries() method returns a new MapIterator instance that contain pair [key => value] of each element of the Map.
$myMap = new Map(['foo' => 'bar']);
$iterator = $myMap->entries();
// [object MapIterator]
$iterator->current();
// ['foo' => 'bar']
The forEach() method executes a provided function once per each element of the Map.
$myMap = new Map(['foo' => 3, 'bar' => 0]);
$myMap->forEach(function (int $value, string $key, Map $map) {
// ...
});
The get() method returns a specified element from map.
$myMap = new Map([1, 'foo' => 'bar', 3]);
$myMap->get('foo');
// 'bar'
$myMap->get(1);
// 3
$myMap->get(2);
// null
The has() method returns TRUE when element with the specified key exists in the Map or FALSE if it does not exist.
$myMap = new Map(['foo' => 'bar']);
$myMap->has('foo');
// true
$myMap->has('baz');
// false
The keys() method returns a new MapIterator instance that contain the keys of each element of the Map.
$myMap = new Map(['foo' => 'bar']);
$iterator = $myMap->keys();
// [object MapIterator]
$iterator->current();
// 'foo'
The set() method adds or updates an element in map with a specified key and a value.
$myMap = new Map();
$myMap->set('foo', 'bar');
// ['foo' => 'bar']
$myMap->set(1, 'baz');
// ['foo' => 'bar', 1 => 'baz']
$myMap->set(null, 'foobar');
// ['foo' => 'bar', 1 => 'baz', 2 => 'foobar']
$myMap->set('foo', 'baz');
// ['foo' => 'baz', 1 => 'baz', 2 => 'foobar']
$myMap->set(null, 'first')->set(null, 'second');
// ['foo' => 'baz', 1 => 'baz', 2 => 'foobar', 3 => 'first', 4 => 'second']
The values() method returns a new MapIterator instance that contain the values of each element of the Map.
$myMap = new Map(['foo' => 'bar']);
$iterator = $myMap->values();
// [object MapIterator]
$iterator->current();
// 'bar'
The size property returns number of elements from the Map.
$myMap = new Map(['foo' => 'bar']);
$myMap->size;
// 1
$myMap->delete('foo');
$myMap->size;
// 0