W3cubDocs

/Symfony 4.1

OrderedHashMap

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) {
        // ...
    }
}

Methods

__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}

Details

__construct(array $elements = array())

Creates a new map.

Parameters

array $elements The elements to insert initially

offsetExists($key)

{@inheritdoc}

Parameters

$key

offsetGet($key)

{@inheritdoc}

Parameters

$key

offsetSet($key, $value)

{@inheritdoc}

Parameters

$key
$value

offsetUnset($key)

{@inheritdoc}

Parameters

$key

getIterator()

{@inheritdoc}

count()

{@inheritdoc}

© 2004–2017 Fabien Potencier
Licensed under the MIT License.
https://api.symfony.com/4.1/Symfony/Component/Form/Util/OrderedHashMap.html