The Map class

(PECL ds >= 1.0.0)

Introduction

A Map is a sequential collection of key-value pairs, almost identical to an array used in a similar context. Keys can be any type, but must be unique. Values are replaced if added to the map using the same key.

Strengths

  • Keys and values can be any type, including objects.
  • Supports array syntax (square brackets).
  • Insertion order is preserved.
  • Performance and memory efficiency is very similar to an array.
  • Automatically frees allocated memory when its size drops low enough.

Weaknesses

  • Can’t be converted to an array when objects are used as keys.

Class synopsis

classDs\MapimplementsDs\Collection, ArrayAccess {
constintMIN_CAPACITY = 16;
publicallocate(int$capacity): void
publicapply(callable$callback): void
publiccapacity(): int
publicclear(): void
publiccopy(): Ds\Map
publicdiff(Ds\Map$map): Ds\Map
publicfilter(callable$callback = ?): Ds\Map
publicfirst(): Ds\Pair
publicget(mixed$key, mixed$default = ?): mixed
publichasKey(mixed$key): bool
publichasValue(mixed$value): bool
publicintersect(Ds\Map$map): Ds\Map
publicisEmpty(): bool
publickeys(): Ds\Set
publicksort(callable$comparator = ?): void
publicksorted(callable$comparator = ?): Ds\Map
publiclast(): Ds\Pair
publicmap(callable$callback): Ds\Map
publicmerge(mixed$values): Ds\Map
publicput(mixed$key, mixed$value): void
publicputAll(mixed$pairs): void
publicreduce(callable$callback, mixed$initial = ?): mixed
publicremove(mixed$key, mixed$default = ?): mixed
publicreverse(): void
publicreversed(): Ds\Map
publicskip(int$position): Ds\Pair
publicslice(int$index, int$length = ?): Ds\Map
publicsort(callable$comparator = ?): void
publicsorted(callable$comparator = ?): Ds\Map
publicsum(): int|float
publictoArray(): array
publicunion(Ds\Map$map): Ds\Map
publicxor(Ds\Map$map): Ds\Map
}

Predefined Constants

Ds\Map::MIN_CAPACITY

Changelog

VersionDescription
PECL ds 1.3.0 The class now implements ArrayAccess.

Table of Contents

To Top