Map
class Map implements Collection, ArrayAccess (View source)
Methods
Creates a new instance, using either a traversable object or an array for the initial values.
Allocates enough memory for a required capacity.
Updates all values by applying a callback function to each value in the map.
Returns the current capacity.
Count elements of an object
Removes all values from the collection.
Returns a shallow copy of the collection.
Creates a new map using a callable to determine which pairs to include
Returns the value for a given key, or an optional default value if the key could not be found.
No description
Determines whether the map contains a given key
Determines whether the map contains a given value
Returns whether the collection is empty.
No description
Specify data which should be serialized to JSON
Sorts the map in-place by key, using an optional comparator function.
Returns a copy sorted by key, using an optional comparator function.
Returns the result of applying a callback function to each value of the map.
Returns the result of associating all keys of a given traversable object or array with their corresponding values, combined with the current instance.
Associates a key with a value, overwriting a previous association if one exists.
Associates all key-value pairs of a traversable object or array.
Reduces the map to a single value using a callback function.
Removes and returns a value by key, or return an optional default value if the key could not be found.
Reverses the map in-place.
Returns a subset of the map defined by a starting index and length.
Sorts the map in-place by value, using an optional comparator function.
Returns a copy, sorted by value using an optional comparator function.
Returns the sum of all values in the map.
No description
No description
No description
No description
Details
__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.
A \ B = {x ∈ A | x ∉ B}
Map
filter(callable|null $callback = null)
Creates a new map using a callable to determine which pairs to include
Pair
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.
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.
Traversable
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.
A ∩ B = {x : x ∈ A ∧ x ∈ B}
Note: Values from the current instance will be kept.
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.
Pair
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.
Sequence
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.
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.
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.
Pair
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.
Note: Arrays and objects are considered equal to zero when calculating the sum.
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.
Sequence
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.
A ⊖ B = {x : x ∈ (A \ B) ∪ (B \ A)}
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