class Map implements Collection, ArrayAccess (View source)

Methods

__construct(TValue> ...$values)

Creates a new instance, using either a traversable object or an array for the initial values.

allocate(int $capacity)

Allocates enough memory for a required capacity.

apply(callable $callback)

Updates all values by applying a callback function to each value in the map.

int
capacity()

Returns the current capacity.

int
count()

Count elements of an object

void
clear()

Removes all values from the collection.

TValue>
copy()

Returns a shallow copy of the collection.

Map
diff(Map $map)

Returns the result of removing all keys from the current instance that are present in a given map.

Map
filter(callable|null $callback = null)

Creates a new map using a callable to determine which pairs to include

first()

Returns the first pair in the map

TValue|TDefault
get(TKey $key, TDefault $default = null)

Returns the value for a given key, or an optional default value if the key could not be found.

getIterator()

No description

bool
hasKey(TKey $key)

Determines whether the map contains a given key

bool
hasValue(TValue $value)

Determines whether the map contains a given value

Map
intersect(Map $map)

Creates a new map containing the pairs of the current instance whose keys are also present in the given map. In other words, returns a copy of the current instance with all keys removed that are not also in the other map.

bool
isEmpty()

Returns whether the collection is empty.

array
toArray()

No description

mixed
jsonSerialize()

Specify data which should be serialized to JSON

Set
keys()

Returns a set containing all the keys of the map, in the same order.

ksort(callable|null $comparator = null)

Sorts the map in-place by key, using an optional comparator function.

Map
ksorted(callable|null $comparator = null)

Returns a copy sorted by key, using an optional comparator function.

last()

Returns the last pair of the map.

Map
map(callable $callback)

Returns the result of applying a callback function to each value of the map.

Map
merge(TValue2> $values)

Returns the result of associating all keys of a given traversable object or array with their corresponding values, combined with the current instance.

pairs()

Returns a Ds\Sequence containing all the pairs of the map.

put(TKey $key, TValue $value)

Associates a key with a value, overwriting a previous association if one exists.

putAll(TValue> $pairs)

Associates all key-value pairs of a traversable object or array.

TCarry
reduce(callable $callback, TCarry $initial)

Reduces the map to a single value using a callback function.

TValue|TDefault
remove(TKey $key, TDefault $default = null)

Removes and returns a value by key, or return an optional default value if the key could not be found.

reverse()

Reverses the map in-place.

Map
reversed()

Returns a reversed copy of the map.

skip(int $position)

Returns the pair at a given zero-based position.

Map
slice(int $index, int|null $length = null)

Returns a subset of the map defined by a starting index and length.

sort(callable|null $comparator = null)

Sorts the map in-place by value, using an optional comparator function.

Map
sorted(callable|null $comparator = null)

Returns a copy, sorted by value using an optional comparator function.

float|int
sum()

Returns the sum of all values in the map.

Map
union(Map $map)

Creates a new map using values from the current instance and another map.

values()

Returns a sequence containing all the values of the map, in the same order.

Map
xor(Map $map)

Creates a new map containing keys of the current instance as well as another map, but not of both.

bool
offsetExists(TKey $offset)

No description

mixed
offsetGet(TKey $offset)

No description

void
offsetSet(TKey $offset, TValue $value)

No description

void
offsetUnset(TKey $offset)

No description

Details

__construct(TValue> ...$values)

Creates a new instance, using either a traversable object or an array for the initial values.

Parameters

TValue> ...$values

A traversable object or an array to use for the initial values.

allocate(int $capacity)

Allocates enough memory for a required capacity.

Parameters

int $capacity

The number of values for which capacity should be allocated.

Note: Capacity will stay the same if this value is less than or equal to the current capacity.

Capacity will always be rounded up to the nearest power of 2.

apply(callable $callback)

Updates all values by applying a callback function to each value in the map.

Parameters

callable $callback

int capacity()

Returns the current capacity.

Return Value

int

int count()

Since: 5.1

Count elements of an object

Return Value

int

The custom count as an integer.

The return value is cast to an integer.

void clear()

Removes all values from the collection.

Return Value

void

TValue> copy()

Returns a shallow copy of the collection.

Return Value

TValue>

Map diff(Map $map)

Returns the result of removing all keys from the current instance that are present in a given map.

A \ B = {x ∈ A | x ∉ B}

Parameters

Map $map

The map containing the keys to exclude in the resulting map.

Return Value

Map

The result of removing all keys from the current instance that are present in a given map.

Map filter(callable|null $callback = null)

Creates a new map using a callable to determine which pairs to include

Parameters

callable|null $callback

Return Value

Map

Pair first()

Returns the first pair in the map

Return Value

Pair

The first pair in the map.

Exceptions

UnderflowException

TValue|TDefault get(TKey $key, TDefault $default = null)

Returns the value for a given key, or an optional default value if the key could not be found.

Note: Keys of type object are supported. If an object implements Ds\Hashable, equality will be determined by the object's equals function. If an object does not implement Ds\Hashable, objects must be references to the same instance to be considered equal.

Note: You can also use array syntax to access values by key, eg. $map["key"].

Caution: Be careful when using array syntax. Scalar keys will be coerced to integers by the engine. For example, $map["1"] will attempt to access int(1), while $map->get("1") will correctly look up the string key.

Parameters

TKey $key

The key to look up.

TDefault $default

The optional default value, returned if the key could not be found.

Return Value

TValue|TDefault

The value mapped to the given key, or the default value if provided and the key could not be found in the map.

Exceptions

OutOfBoundsException

Traversable getIterator()

No description

Return Value

Traversable

An instance of an object implementing Iterator or Traversable

bool hasKey(TKey $key)

Determines whether the map contains a given key

Parameters

TKey $key

The key to look for.

Return Value

bool

Returns TRUE if the key could found, FALSE otherwise.

bool hasValue(TValue $value)

Determines whether the map contains a given value

Parameters

TValue $value

The value to look for.

Return Value

bool

Returns TRUE if the value could found, FALSE otherwise.

Map intersect(Map $map)

Creates a new map containing the pairs of the current instance whose keys are also present in the given map. In other words, returns a copy of the current instance with all keys removed that are not also in the other map.

A ∩ B = {x : x ∈ A ∧ x ∈ B}

Note: Values from the current instance will be kept.

Parameters

Map $map

The other map, containing the keys to intersect with.

Return Value

Map

The key intersection of the current instance and another map.

bool isEmpty()

Returns whether the collection is empty.

Return Value

bool

array toArray()

No description

Return Value

array

An array containing all the values in the same order as the collection.

mixed jsonSerialize()

Since: 5.4

Specify data which should be serialized to JSON

Return Value

mixed

data which can be serialized by json_encode, which is a value of any type other than a resource.

Set keys()

Returns a set containing all the keys of the map, in the same order.

Return Value

Set

A Ds\Set containing all the keys of the map.

ksort(callable|null $comparator = null)

Sorts the map in-place by key, using an optional comparator function.

Parameters

callable|null $comparator

Map ksorted(callable|null $comparator = null)

Returns a copy sorted by key, using an optional comparator function.

Parameters

callable|null $comparator

Return Value

Map

Returns a copy of the map, sorted by key.

Pair last()

Returns the last pair of the map.

Return Value

Pair

The last pair of the map.

Exceptions

UnderflowException

Map map(callable $callback)

Returns the result of applying a callback function to each value of the map.

Parameters

callable $callback

Return Value

Map

The result of applying a callback to each value in the map.

Note: The keys and values of the current instance won't be affected.

Map merge(TValue2> $values)

Returns the result of associating all keys of a given traversable object or array with their corresponding values, combined with the current instance.

Parameters

TValue2> $values

A traversable object or an array.

Return Value

Map

The result of associating all keys of a given traversable object or array with their corresponding values, combined with the current instance.

Note: The current instance won't be affected.

Sequence pairs()

Returns a Ds\Sequence containing all the pairs of the map.

Return Value

Sequence

Ds\Sequence containing all the pairs of the map.

put(TKey $key, TValue $value)

Associates a key with a value, overwriting a previous association if one exists.

Parameters

TKey $key

The key to associate the value with.

TValue $value

The value to be associated with the key.

Note: Keys of type object are supported. If an object implements Ds\Hashable, equality will be determined by the object's equals function. If an object does not implement Ds\Hashable, objects must be references to the same instance to be considered equal.

Note: You can also use array syntax to associate values by key, eg. $map["key"] = $value.

Caution: Be careful when using array syntax. Scalar keys will be coerced to integers by the engine. For example, $map["1"] will attempt to access int(1), while $map->get("1") will correctly look up the string key.

putAll(TValue> $pairs)

Associates all key-value pairs of a traversable object or array.

Note: Keys of type object are supported. If an object implements Ds\Hashable, equality will be determined by the object's equals function. If an object does not implement Ds\Hashable, objects must be references to the same instance to be considered equal.

Parameters

TValue> $pairs

traversable object or array.

TCarry reduce(callable $callback, TCarry $initial)

Reduces the map to a single value using a callback function.

Parameters

callable $callback
TCarry $initial

The initial value of the carry value. Can be NULL.

Return Value

TCarry

TValue|TDefault remove(TKey $key, TDefault $default = null)

Removes and returns a value by key, or return an optional default value if the key could not be found.

Parameters

TKey $key

The key to remove.

TDefault $default

The optional default value, returned if the key could not be found.

Note: Keys of type object are supported. If an object implements Ds\Hashable, equality will be determined by the object's equals function. If an object does not implement Ds\Hashable, objects must be references to the same instance to be considered equal.

Note: You can also use array syntax to access values by key, eg. $map["key"].

Caution: Be careful when using array syntax. Scalar keys will be coerced to integers by the engine. For example, $map["1"] will attempt to access int(1), while $map->get("1") will correctly look up the string key.

Return Value

TValue|TDefault

The value that was removed, or the default value if provided and the key could not be found in the map.

Exceptions

OutOfBoundsException

reverse()

Reverses the map in-place.

Map reversed()

Returns a reversed copy of the map.

Return Value

Map

A reversed copy of the map.

Note: The current instance is not affected.

Pair skip(int $position)

Returns the pair at a given zero-based position.

Parameters

int $position

The zero-based positional index to return.

Return Value

Pair

Returns the Ds\Pair at the given position.

Exceptions

OutOfRangeException

Map slice(int $index, int|null $length = null)

Returns a subset of the map defined by a starting index and length.

Parameters

int $index

The index at which the range starts. If positive, the range will start at that index in the map. If negative, the range will start that far from the end.

int|null $length

If a length is given and is positive, the resulting map will have up to that many pairs in it. If a length is given and is negative, the range will stop that many pairs from the end. If the length results in an overflow, only pairs up to the end of the map will be included. If a length is not provided, the resulting map will contain all pairs between the index and the end of the map.

Return Value

Map

A subset of the map defined by a starting index and length.

sort(callable|null $comparator = null)

Sorts the map in-place by value, using an optional comparator function.

Parameters

callable|null $comparator

Map sorted(callable|null $comparator = null)

Returns a copy, sorted by value using an optional comparator function.

Parameters

callable|null $comparator

Return Value

Map

float|int sum()

Returns the sum of all values in the map.

Note: Arrays and objects are considered equal to zero when calculating the sum.

Return Value

float|int

The sum of all the values in the map as either a float or int depending on the values in the map.

Map union(Map $map)

Creates a new map using values from the current instance and another map.

A ∪ B = {x: x ∈ A ∨ x ∈ B}

Note: Values of the current instance will be overwritten by those provided where keys are equal.

Parameters

Map $map

The other map, to combine with the current instance.

Return Value

Map

Sequence values()

Returns a sequence containing all the values of the map, in the same order.

Return Value

Sequence

A Ds\Sequence containing all the values of the map.

Map xor(Map $map)

Creates a new map containing keys of the current instance as well as another map, but not of both.

A ⊖ B = {x : x ∈ (A \ B) ∪ (B \ A)}

Parameters

Map $map

The other map.

Return Value

Map

A new map containing keys in the current instance as well as another map, but not in both.

bool offsetExists(TKey $offset)

No description

Parameters

TKey $offset

An offset to check for.

Return Value

bool

true on success or false on failure.

The return value will be casted to boolean if non-boolean was returned.

mixed offsetGet(TKey $offset)

No description

Parameters

TKey $offset

The offset to retrieve.

Return Value

mixed

Can return all value types.

void offsetSet(TKey $offset, TValue $value)

No description

Parameters

TKey $offset

The offset to assign the value to.

TValue $value

The value to set.

Return Value

void

void offsetUnset(TKey $offset)

No description

Parameters

TKey $offset

The offset to unset.

Return Value

void