phpday 2027 - Call for Papers

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

class Ds\Map implements Ds\Collection, ArrayAccess {
/* Constants */
const int MIN_CAPACITY = 8;
/* Methods */
public function allocate(int $capacity): void
public function apply(callable $callback): void
public function capacity(): int
public function clear(): void
public function copy(): Ds\Map
public function diff(Ds\Map $map): Ds\Map
public function filter(callable $callback = ?): Ds\Map
public function first(): Ds\Pair
public function get(mixed $key, mixed $default = ?): mixed
public function hasKey(mixed $key): bool
public function hasValue(mixed $value): bool
public function intersect(Ds\Map $map): Ds\Map
public function isEmpty(): bool
public function keys(): Ds\Set
public function ksort(callable $comparator = ?): void
public function ksorted(callable $comparator = ?): Ds\Map
public function last(): Ds\Pair
public function map(callable $callback): Ds\Map
public function merge(mixed $values): Ds\Map
public function pairs(): Ds\Sequence
public function put(mixed $key, mixed $value): void
public function putAll(mixed $pairs): void
public function reduce(callable $callback, mixed $initial = ?): mixed
public function remove(mixed $key, mixed $default = ?): mixed
public function reverse(): void
public function reversed(): Ds\Map
public function skip(int $position): Ds\Pair
public function slice(int $index, int $length = ?): Ds\Map
public function sort(callable $comparator = ?): void
public function sorted(callable $comparator = ?): Ds\Map
public function sum(): int|float
public function toArray(): array
public function union(Ds\Map $map): Ds\Map
public function values(): Ds\Sequence
public function xor(Ds\Map $map): Ds\Map
}

Predefined Constants

Ds\Map::MIN_CAPACITY

Changelog

Version Description
PECL ds 1.3.0 The class now implements ArrayAccess.
PECL ds 1.2.0 Ds\Map::MIN_CAPACITY changed from 16 to 8.

Table of Contents

User Contributed Notes

There are no user contributed notes for this page.
Adding new notes is disabled. Please see the links in the Found A Problem? section.
To Top