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 = [])

Creates a new map.

offsetExists($key)

{@inheritdoc}

offsetGet($key)

{@inheritdoc}

offsetSet($key, $value)

{@inheritdoc}

offsetUnset($key)

{@inheritdoc}

getIterator()

{@inheritdoc}

count()

{@inheritdoc}

Details

at line 95
__construct(array $elements = [])

Creates a new map.

Parameters

array $elements The elements to insert initially

at line 104
offsetExists($key)

{@inheritdoc}

Parameters

$key

at line 112
offsetGet($key)

{@inheritdoc}

Parameters

$key

at line 124
offsetSet($key, $value)

{@inheritdoc}

Parameters

$key
$value

at line 145
offsetUnset($key)

{@inheritdoc}

Parameters

$key

at line 162
getIterator()

{@inheritdoc}

at line 170
count()

{@inheritdoc}