php8.5
Home/ Manual/ reference / ds/ The Map class

The Map class

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.

Intro

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 { }

Predefined Constants

Ds\Map::MIN_CAPACITY

Changelog

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

Map

Source: reference/ds/ds.map.xml · from the official PHP manual (php/doc-en)