class OrderedHashMap implements ArrayAccess, IteratorAggregate, Countable
A hash map which keeps track of deletions and additions.
Like in associative arrays, elements can be mapped to integer or string keys. Unlike associative arrays, the map keeps track of the order in which keys were added and removed. This order is reflected during iteration.
The map supports concurrent modification during iteration. That means that you can insert and remove elements from within a foreach loop and the iterator will reflect those changes accordingly.
While elements that are added during the loop are recognized by the iterator, changed elements are not. Otherwise the loop could be infinite if each loop changes the current element:
$map = new OrderedHashMap();
$map[1] = 1;
$map[2] = 2;
$map[3] = 3;
foreach ($map as $index => $value) {
echo "$index: $value\n"
if (1 === $index) {
$map[1] = 4;
$map[] = 5;
}
}
print_r(iterator_to_array($map));
// => 1: 1
// 2: 2
// 3: 3
// 4: 5
// Array
// (
// [1] => 4
// [2] => 2
// [3] => 3
// [4] => 5
// )
The map also supports multiple parallel iterators. That means that you can nest foreach loops without affecting each other's iteration:
foreach ($map as $index => $value) {
foreach ($map as $index2 => $value2) {
// ...
}
}
__construct(array $elements = array()) Creates a new map. | ||
offsetExists($key) {@inheritdoc} | ||
offsetGet($key) {@inheritdoc} | ||
offsetSet($key, $value) {@inheritdoc} | ||
offsetUnset($key) {@inheritdoc} | ||
getIterator() {@inheritdoc} | ||
count() {@inheritdoc} |
Creates a new map.
array | $elements | The elements to insert initially |
{@inheritdoc}
$key |
{@inheritdoc}
$key |
{@inheritdoc}
$key | ||
$value |
{@inheritdoc}
$key |
{@inheritdoc}
{@inheritdoc}
© 2004–2017 Fabien Potencier
Licensed under the MIT License.
https://api.symfony.com/4.1/Symfony/Component/Form/Util/OrderedHashMap.html