OrderedHashMap
in package
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) {
// ...
}
}
Tags
Interfaces, Classes, Traits and Enums
- ArrayAccess
- IteratorAggregate
- Countable
Table of Contents
- $elements : array<string|int, mixed>
- The elements of the map, indexed by their keys.
- $managedCursors : array<string|int, mixed>
- References to the cursors of all open iterators.
- $orderedKeys : array<string|int, mixed>
- The keys of the map in the order in which they were inserted or changed.
- __construct() : mixed
- Creates a new map.
- count() : int
- getIterator() : Traversable
- offsetExists() : bool
- offsetGet() : mixed
- {@inheritdoc}
- offsetSet() : void
- {@inheritdoc}
- offsetUnset() : void
- {@inheritdoc}
Properties
$elements
The elements of the map, indexed by their keys.
private
array<string|int, mixed>
$elements
= []
$managedCursors
References to the cursors of all open iterators.
private
array<string|int, mixed>
$managedCursors
= []
$orderedKeys
The keys of the map in the order in which they were inserted or changed.
private
array<string|int, mixed>
$orderedKeys
= []
Methods
__construct()
Creates a new map.
public
__construct([array<string|int, mixed> $elements = [] ]) : mixed
Parameters
- $elements : array<string|int, mixed> = []
-
The elements to insert initially
Return values
mixed —count()
public
count() : int
Return values
int —getIterator()
public
getIterator() : Traversable
Return values
Traversable —offsetExists()
public
offsetExists(mixed $key) : bool
Parameters
- $key : mixed
Return values
bool —offsetGet()
{@inheritdoc}
public
offsetGet(mixed $key) : mixed
Parameters
- $key : mixed
Return values
mixed —offsetSet()
{@inheritdoc}
public
offsetSet(mixed $key, mixed $value) : void
Parameters
- $key : mixed
- $value : mixed
Return values
void —offsetUnset()
{@inheritdoc}
public
offsetUnset(mixed $key) : void
Parameters
- $key : mixed