Cheatsheet β
A scannable, example-driven tour of the API. For exact signatures and edge cases, see the API Reference.
How to read this β
- Examples use the factory functions β
listOf,setOf,mapOf(immutable) and theirmutable*variants. - Emoji are just sample values; plain numbers appear where the operation is arithmetic.
- A trailing comment shows the result's contents:
[...]is a list,{...}is a set or map. - Unless noted, transformation and ordering methods return a new immutable collection β the source is never changed, even when it is mutable.
- Predicates/selectors on List/Set receive
(element, int $index); on Map,(value, key). - Some examples reuse
$isFruit = fn($e) => in_array($e, ['π','π','π','π'], true);.
Creating collections β
listOf(['π','π','π']); // ImmutableList
mutableListOf([1, 2, 3]); // MutableList
setOf(['π','π','π']); // ImmutableSet {π, π} β duplicates dropped
mutableSetOf(['a','b']); // MutableSet
mapOf(['π' => 3, 'π' => 5]); // ImmutableMap (fruit β count)
mutableMapOf(['x' => 10]); // MutableMap
mapOfPairs([['1','a'], [$user,'b']]); // preserves exact key types
stringMapOf(['name' => 'Amy']); // optimized for string keys
intMapOf([1 => 'a', 2 => 'b']); // optimized for int keys
listOf(fn() => loadRows()); // lazy β loaded on first accessList & Set β
Inherited from Collection<E>, so available on both lists and sets.
Element access β
listOf(['π','π','π'])->first(); // π
listOf([])->firstOrNull(); // null
listOf(['π','π','π'])->last(); // π
listOf(['π'])->single(); // π β throws unless exactly one element
listOf(['π','π','π'])->find($isFruit); // π (first match)
listOf(['π','π','π'])->findLast($isFruit); // π β last match
listOf(['π₯','π₯¦'])->expect($isFruit); // throws NoSuchElementException
listOf(['π','π','π'])->random(); // e.g. πQuerying β
listOf(['π','π','π'])->contains('π'); // true
listOf(['π','π','π'])->containsAll(['π','π']); // true
listOf(['π','π'])->all($isFruit); // true
listOf(['π','π₯'])->any($isFruit); // true
listOf(['π₯','π₯¦'])->none($isFruit); // true
listOf(['π','π','π'])->count(); // 3
listOf(['π','π₯','π'])->countWhere($isFruit); // 2
listOf([])->isEmpty(); // trueAggregation β
Arithmetic, so these use plain numbers. fold starts from an initial value, reduce from the first element.
listOf([1, 2, 3])->fold(10, fn($acc, $n) => $acc + $n); // 16
listOf([1, 2, 3])->reduce(fn($acc, $n) => $acc + $n); // 6
listOf([])->reduceOrNull(fn($a, $b) => $a + $b); // null
listOf([10, 20, 30])->sum(); // 60
listOf([10, 20, 30])->avg(); // 20.0
listOf([3, 1, 2])->min(); // 1
listOf([3, 1, 2])->max(); // 3min/max return the element; minOf/maxOf return the selector value.
$people = listOf([
['name' => 'Amy', 'age' => 30],
['name' => 'Bob', 'age' => 25],
]);
$people->min(fn($p) => $p['age']); // ['name' => 'Bob', 'age' => 25]
$people->minOf(fn($p) => $p['age']); // 25listOf(['π','π','π'])->joinToString(', '); // "π, π, π"
listOf(['π','π'])->joinToString(', ', '[', ']'); // "[π, π]"
listOf(['π','π','π'])->countBy(fn($e) => $e); // {π: 2, π: 1}Transformation β
listOf(['π','π₯','π','π₯¦'])->filter($isFruit); // [π, π]
listOf(['π', null, 'π'])->filterNotNull(); // [π, π]
listOf($animals)->filterInstanceOf(Dog::class); // ImmutableList<Dog>
listOf(['π','π','π'])->map(fn($e) => 'π¦'); // [π¦, π¦, π¦]
listOf([1, 2, 3, 4])->mapNotNull(fn($n) => $n % 2 ? null : $n); // [2, 4]
listOf([['π','π'], ['π']])->flatMap(fn($g) => $g); // [π, π, π]
listOf([['π','π'], ['π']])->flatten(); // [π, π, π]listOf(['π','π','π','π'])->takeFirst(2); // [π, π]
listOf(['π','π','π','π'])->takeLast(2); // [π, π]
listOf(['π','π','π','π'])->dropFirst(2); // [π, π]
listOf(['π','π','π','π'])->dropLast(2); // [π, π]
listOf(['π','π','π₯','π'])->takeWhile($isFruit); // [π, π]
listOf(['π','π','π₯','π'])->dropWhile($isFruit); // [π₯, π]
listOf(['π','π','π','π'])->distinct(); // [π, π, π]
$people->distinctBy(fn($p) => $p['age']); // first of each distinct agechunked splits into fixed-size pieces; windowed slides a window across.
listOf([1, 2, 3, 4, 5])->chunked(2); // [[1,2], [3,4], [5]]
listOf([1, 2, 3, 4])->windowed(2); // [[1,2], [2,3], [3,4]]
listOf([1, 2, 3, 4, 5])->windowed(3, step: 2); // [[1,2,3], [3,4,5]]zip pairs two lists (stops at the shorter); zipWithNext pairs adjacent elements; unzip is the inverse of zip.
listOf(['π§','π©'])->zip(['π©','π']); // [[π§,π©], [π©,π]]
listOf(['π','π','π'])->zipWithNext(); // [[π,π], [π,π]]
listOf([['π§','π©'], ['π©','π']])->unzip(); // [[π§,π©], [π©,π]]partition splits into [matching, rest]; groupBy returns a Map of lists.
listOf(['π','π₯','π','π₯¦'])->partition($isFruit); // [[π,π], [π₯,π₯¦]]
listOf([1, 2, 3, 4])->groupBy(fn($n) => $n % 2 ? 'odd' : 'even');
// {odd: [1, 3], even: [2, 4]}Set operations always return an ImmutableSet.
listOf(['π','π','π'])->intersect(['π','π','π']); // {π, π}
listOf(['π','π'])->union(['π','π']); // {π, π, π}
listOf(['π','π','π'])->subtract(['π','π']); // {π}Ordering β
listOf([3, 1, 2])->sorted(); // [1, 2, 3]
listOf([3, 1, 2])->sortedDesc(); // [3, 2, 1]
$people->sortedBy(fn($p) => $p['age']); // youngest β oldest
$people->sortedByDesc(fn($p) => $p['age']); // oldest β youngest
listOf([3, 1, 2])->sortedWith(fn($a, $b) => $a <=> $b); // [1, 2, 3]
listOf(['π','π','π'])->reversed(); // [π, π, π]
listOf(['π','π','π'])->shuffled(); // random orderIteration & conversion β
listOf(['π','π'])->forEach(fn($e) => send($e)); // returns the list
listOf(['π','π','π'])->toSet(); // {π, π}
setOf(['π','π'])->toList(); // [π, π]
listOf(['π','π'])->toArray(); // ['π', 'π'] β native PHP array
listOf(['Amy','Bob'])->toMap(fn($n) => $n[0]); // {A: 'Amy', B: 'Bob'}
listOf([1, 2])->toMutable(); // MutableList β always a fresh copy
mutableListOf([1, 2])->toImmutable(); // ImmutableListList only β
Positional access by integer index (Set has none). slice(from, to) is from-inclusive, to-exclusive.
listOf(['π','π','π'])->get(1); // π β alias: $list[1]
listOf(['π','π','π'])->getOrNull(9); // null β $list[9] ?? null
listOf(['π','π'])->getOrDefault(9, 'π«'); // π«
listOf(['π','π','π'])->indexOf('π'); // 1 (-1 if absent)
listOf([10, 20, 30])->indexOfFirst(fn($n) => $n > 15); // 1
listOf(['π','π','π','π'])->slice(1, 3); // [π, π]Mutation adds set(index, value), removeAt(index), removeEvery(value).
Set only β
Sets hold unique elements (duplicates dropped on insert) and add no methods of their own. One twist: addFirst() uses move-to-front semantics.
setOf(['π','π','π']); // {π, π}
mutableSetOf(['π','π','π'])->addFirst('π'); // {π, π, π}Map β
A key-value collection with strict key handling β keys are never silently cast. Predicates receive (value, key).
Keys, values & entries β
A map exposes three live, read-only views as properties: $keys is a Set, $values is a Collection, and $entries is a Set of MapEntry. Keys are unique (so a Set); values may repeat (so a Collection).
$colors = mapOf(['π' => 'red', 'π' => 'yellow', 'π' => 'purple']);
$colors->keys; // Set<string> {π, π, π}
$colors->values; // Collection<string> [red, yellow, purple]
$colors->entries; // Set<MapEntry> {πβred, πβyellow, πβpurple}Split apart, the same map decomposes like this:
$colors entry | ->keys (Set) | ->values (Collection) |
|---|---|---|
| π β red | π | red |
| π β yellow | π | yellow |
| π β purple | π | purple |
Access & querying β
mapOf(['π' => 3])->get('π'); // 3 β alias: $map['π']
mapOf(['π' => 3])->getOrNull('π'); // null
mapOf(['π' => 3])->getOrDefault('π', 0); // 0
mutableMapOf(['π' => 3])->getOrPut('π', fn() => 5); // 5 (stored)
mapOf(['π' => 3])->containsKey('π'); // true
mapOf(['π' => 3])->containsValue(3); // true
mapOf(['π' => 3, 'π' => 5])->count(); // 2
mapOf(['π' => 3, 'π' => 5])->all(fn($v, $k) => $v > 0); // trueTransformation β
mapOf(['π' => 3, 'π' => 5])->filter(fn($v, $k) => $v > 4); // {π: 5}
mapOf(['π' => 3, 'π' => 5])->filterValues(fn($v) => $v > 4); // {π: 5}
mapOf(['π' => 3])->mapValues(fn($v, $k) => $v * 10); // {π: 30}
mapOf(['π' => 3])->mapKeys(fn($v, $k) => "fruit:$k"); // {fruit:π: 3}
mapOf(['π' => 3, 'π' => 5])->flip(); // {3: π, 5: π}
mapOf(['π' => 3, 'π' => 5])->map(fn($v, $k) => "$k=$v");
// ['π=3', 'π=5'] β a listOrdering & conversion β
mapOf(['π' => 5, 'π' => 3])->sortedByValue(); // {π: 3, π: 5}
mapOf(['π' => 5, 'π' => 3])->reversed(); // {π: 3, π: 5}
mapOf(['π' => 3])->toArray(); // ['π' => 3]
mapOf(['π' => 3])->toPairs(); // [['π', 3]]Mutation β
mutableMapOf(['π' => 3])->put('π', 5); // adds/replaces
mutableMapOf(['π' => 3])->putIfAbsent('π', 9); // no-op β key exists
mutableMapOf(['π' => 3, 'π' => 5])->remove('π'); // {π: 5}Also: putFirst, putAll, putAllPairs, removeIf, removeIfKey, removeIfValue, removeNullValues, clear, plus the in-place sortByKey / sortByValue / β¦ family.
Mutable vs. immutable β
Same method names, different behavior. On a mutable collection they change it in place and return it (handy for chaining); on an immutable collection they return a new collection and leave the original untouched.
$m = mutableListOf(['π','π']);
$m->add('π'); // $m is now [π, π, π]
$i = listOf(['π','π']);
$new = $i->add('π'); // $i stays [π, π]; $new is [π, π, π]Shared mutators (List & Set): add, addFirst, addAll, removeElement, removeFirst, removeLast, removeIf, removeAll, retainAll, clear, and the in-place ordering sort / sortBy / sortByDesc / sortWith / reverse / shuffle.
Tracking changes β
tracked() wraps a mutable collection so each mutation reports whether it actually changed anything:
$t = mutableSetOf(['π','π'])->tracked();
$t->add('π')->changed; // true
$t->add('π')->changed; // false β already present