Libraries / collections
Overview
Fluent collection and array pipeline utilities for HolyPHP.
bundled — works offline
array
pipeline
map
Quick start
import "collections";
$result = collect([1, 2, 2, 3, 4, 5, 6, 7, 8])
->unique()
->filter(function($x) { return $x % 2 === 0; })
->map(function($x) { return $x * 10; })
->skip(1)
->take(2)
->toArray();
// $result => [40, 60]
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages ):
hphp install collections
This adds the library to hphp.yaml, installs it into .hphp/packages/collections/ and pins the exact version in hphp.lock:
require:
collections: "^1.0"
Use it
import "collections";
collections is bundled with the compiler, so import "collections"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install collections # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install collections
Documentation
API Reference
collect($items = []): Collection
Wraps an array (or clones an existing Collection) into a fluent Collection instance.
Collection Class
Every transformation method returns a new Collection instance (immutable pipeline), while terminal methods return a value:
map($fn): Collection — Transforms each item using $fn($value, $key).
filter($fn = null): Collection — Keeps items where $fn($value, $key) is truthy (or truthy values if $fn is null).
reduce($fn, $initial = null) — Reduces items to a single value using $fn($acc, $value, $key).
groupBy($keyOrFn): Collection — Groups items by an associative array key or callback $fn($item, $key).
chunk(int $size): Collection — Splits items into chunks of length $size.
flatten(int $depth = -1): Collection — Flattens nested arrays up to $depth levels (-1 for full flattening).
pluck($key): Collection — Extracts $key from each associative array item.
unique(): Collection — Removes duplicate values (strict === comparison).
zip($other): Collection — Pairs elements with another array or Collection up to the shorter length.
take(int $n): Collection — Takes the first $n items.
skip(int $n): Collection — Skips the first $n items.
first($default = null) — Returns the first item or $default if empty.
last($default = null) — Returns the last item or $default if empty.
count(): int — Returns the number of items.
isEmpty(): bool — Returns true if the collection has no items.
toArray() — Returns the underlying array.
Standalone Helpers
All operations are also available as standalone collections_* functions:
collections_map($arr, $fn)
collections_filter($arr, $fn = null)
collections_reduce($arr, $fn, $initial = null)
collections_groupBy($arr, $keyOrFn)
collections_chunk($arr, int $size)
collections_flatten($arr, int $depth = -1)
collections_pluck($arr, $key)
collections_unique($arr)
collections_zip($arr, $other)
collections_take($arr, int $n)
collections_skip($arr, int $n)
collections_first($arr, $default = null)
collections_last($arr, $default = null)
collections_count($arr): int
collections_toArray($value)
Error Handling
All functions validate their inputs and throw Exception with clear collections: ... messages:
collections_chunk($arr, 0) → collections: chunk() size must be a positive integer greater than 0, got 0
collections_take($arr, -1) → collections: take() count must be >= 0, got -1
collections_skip($arr, -1) → collections: skip() count must be >= 0, got -1
collections_flatten($arr, -2) → collections: flatten() depth must be -1 (infinite) or >= 0, got -2
Passing a non-array to collect() or any collections_* helper throws an Exception describing the expected type.
Examples
01 Quick start
Bundled with the compiler — import "collections"; works offline in any file, or add it to a project with hphp install collections.
import "collections";
$result = collect([1, 2, 2, 3, 4, 5, 6, 7, 8])
->unique()
->filter(function($x) { return $x % 2 === 0; })
->map(function($x) { return $x * 10; })
->skip(1)
->take(2)
->toArray();
// $result => [40, 60]
Try it hphp install collections
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_collections.hphp.