The SPL Data Structures

The Standard PHP Library, compiled into every PHP build, supplies classic data structures as classes that already implement Countable, Iterator or ArrayAccess.

The SPL data structures
Class Behavior Typical use
SplStack, SplQueue LIFO and FIFO over a linked list Undo history, job lists
SplMinHeap, SplMaxHeap Smallest or largest on top Top-N, merging sorted runs
SplPriorityQueue Extracts by priority Dispatch order, schedulers
SplFixedArray Integer keys 0..n-1, fixed size Large numeric tables
SplObjectStorage Objects as keys, with data Visited sets, identity maps
A dispatch queue, an object set and a memory comparisonPHP
<?php
$q = new SplPriorityQueue();
foreach (['standard' => 1, 'express' => 3, 'next-day' => 2] as $o => $p) $q->insert($o, $p);
echo implode(' > ', iterator_to_array($q, false)), ' (left: ', count($q), ")\n";
$seen = new SplObjectStorage();
$order = new stdClass();
$seen[$order] = 'emailed';                   // offsetSet(); attach() is deprecated in 8.5
echo count($seen), ' ', $seen[$order], ' ', json_encode(isset($seen[new stdClass()])), "\n";
$n = 100_000;
$m = memory_get_usage(); $arr = []; for ($i = 0; $i < $n; $i++) $arr[] = $i;
$arrKb = (memory_get_usage() - $m) >> 10;
$m = memory_get_usage(); $f = new SplFixedArray($n); for ($i = 0; $i < $n; $i++) $f[$i] = $i;
printf("array %d KB, SplFixedArray %d KB\n", $arrKb, (memory_get_usage() - $m) >> 10);
Output
express > next-day > standard (left: 0)
1 emailed false
array 2052 KB, SplFixedArray 1564 KB

Iterating a heap or priority queue consumes it, hence left: 0. SplFixedArray is allocated at exactly n slots, where the array grew by doubling to 131,072; since PHP 8.2 a list-like array stores bare values without hash buckets, so the gap is modest. For SplObjectStorage, PHP 8.5 deprecates attach(), detach() and contains() in favor of [], unset() and isset().