Collection Documentation

ExCollectionInterface extends CollectionInterface

The extended collection contract: {@see CollectionInterface} plus the array-style helpers (`shift`, `search`, `splice`, `slice`, `sort`, `diff`, `intersect`, `walk`, `reduce`, `unique`, …). Implemented by {@see CollectionTrait}.

Table of Contents

Methods

__debugInfo()  : array<string|int, mixed>
Debug representation for `var_dump()`.
__serialize()  : array<string|int, mixed>
The items, for PHP's native `serialize()`.
__unserialize()  : void
Restores the collection from the payload of PHP's native `unserialize()`.
all()  : bool
Whether every item matches `$callback`.
any()  : bool
Whether at least one item matches `$callback`.
clear()  : void
Removes every item from the collection.
count()  : int
diff()  : ExCollectionInterface
fill()  : self
Fills the collection with an array (or another collection) of items.
filter()  : mixed
Runs a filter callback over the collection and returns a new collection of the items for which it returned true.
find()  : mixed
Returns the first item for which `$callback` returns true, or null.
find_key()  : string|int|null
first()  : mixed
get()  : mixed
Gets an item from the collection whose `$discrim` property equals `$key`.
getIterator()  : Traversable
has()  : bool
Whether an item exists at every one of `$keys`.
intersect()  : ExCollectionInterface
isset()  : bool
Whether an item exists at `$offset`.
jsonSerialize()  : array<string|int, mixed>
keys()  : array<string|int, mixed>
last()  : mixed
map()  : mixed
Returns a new collection with `$callback` applied to each item.
merge()  : self
Merges another collection (or array) into this one.
offsetExists()  : bool
offsetGet()  : mixed
offsetSet()  : void
offsetUnset()  : void
pull()  : mixed
Removes an item at `$key` and returns it, or `$default` when it is absent.
push()  : self
Pushes one or more items onto the collection.
pushItem()  : self
Pushes a single item onto the collection, keyed by its discriminator.
reduce()  : mixed
search()  : string|int|false
Searches for `$needle` and returns its key, or false when not found.
serialize()  : string
Serialises the collection to a JSON string.
set()  : mixed
Sets a value in the collection at `$offset`.
shift()  : array<string|int, mixed>|null
slice()  : ExCollectionInterface
Returns a new collection with the slice `$offset`..`$offset + $length`.
sort()  : ExCollectionInterface
splice()  : ExCollectionInterface
Removes `$length` items starting at `$offset`, inserting `$replacement` in their place. Mutates the collection and returns it.
toArray()  : array<string|int, mixed>
unique()  : ExCollectionInterface
unserialize()  : void
Restores the collection from a JSON string produced by {@see serialize()}.
values()  : array<string|int, mixed>
walk()  : ExCollectionInterface

Methods

__debugInfo()

Debug representation for `var_dump()`.

public __debugInfo() : array<string|int, mixed>
Return values
array<string|int, mixed>

__serialize()

The items, for PHP's native `serialize()`.

public __serialize() : array<string|int, mixed>
Return values
array<string|int, mixed>

__unserialize()

Restores the collection from the payload of PHP's native `unserialize()`.

public __unserialize(array<string|int, mixed> $data) : void
Parameters
$data : array<string|int, mixed>

all()

Whether every item matches `$callback`.

public all(callable $callback) : bool
Parameters
$callback : callable
Return values
bool

any()

Whether at least one item matches `$callback`.

public any(callable $callback) : bool
Parameters
$callback : callable
Return values
bool

filter()

Runs a filter callback over the collection and returns a new collection of the items for which it returned true.

public filter(callable $callback) : mixed
Parameters
$callback : callable

find()

Returns the first item for which `$callback` returns true, or null.

public find(callable $callback) : mixed
Parameters
$callback : callable

find_key()

public find_key(callable $callback) : string|int|null
Parameters
$callback : callable
Return values
string|int|null —

The key of the first item matching $callback, or null.

first()

public first() : mixed
Return values
mixed —

The first item in the collection, or null when empty.

get()

Gets an item from the collection whose `$discrim` property equals `$key`.

public get(string $discrim, mixed $key) : mixed
Parameters
$discrim : string

The property to match on.

$key : mixed

The value to match.

Return values
mixed —

The matching item, or null.

getIterator()

public getIterator() : Traversable
Tags
inheritDoc
Return values
Traversable

has()

Whether an item exists at every one of `$keys`.

public has(mixed ...$keys) : bool
Parameters
$keys : mixed
Return values
bool

isset()

Whether an item exists at `$offset`.

public isset(mixed $offset) : bool
Parameters
$offset : mixed
Return values
bool

jsonSerialize()

public jsonSerialize([bool $assoc = true ]) : array<string|int, mixed>
Parameters
$assoc : bool = true

Whether to keep string keys.

Tags
inheritDoc
Return values
array<string|int, mixed>

keys()

public keys() : array<string|int, mixed>
Return values
array<string|int, mixed> —

The collection's keys.

last()

public last() : mixed
Return values
mixed —

The last item in the collection, or null when empty.

map()

Returns a new collection with `$callback` applied to each item.

public map(callable $callback) : mixed
Parameters
$callback : callable

offsetExists()

public offsetExists(mixed $offset) : bool
Parameters
$offset : mixed
Tags
inheritDoc
Return values
bool

offsetGet()

public offsetGet(mixed $offset) : mixed
Parameters
$offset : mixed
Tags
inheritDoc
Attributes
#[ReturnTypeWillChange]

offsetSet()

public offsetSet(mixed $offset, mixed $value) : void
Parameters
$offset : mixed
$value : mixed
Tags
inheritDoc

offsetUnset()

public offsetUnset(mixed $offset) : void
Parameters
$offset : mixed
Tags
inheritDoc

pull()

Removes an item at `$key` and returns it, or `$default` when it is absent.

public pull(mixed $key[, mixed $default = null ]) : mixed
Parameters
$key : mixed
$default : mixed = null

push()

Pushes one or more items onto the collection.

public push(mixed ...$items) : self
Parameters
$items : mixed
Return values
self

pushItem()

Pushes a single item onto the collection, keyed by its discriminator.

public pushItem(mixed $item) : self
Parameters
$item : mixed
Return values
self

reduce()

public reduce(callable $callback[, mixed $initial = null ]) : mixed
Parameters
$callback : callable
$initial : mixed = null
Return values
mixed —

The final value of $carry.

Searches for `$needle` and returns its key, or false when not found.

public search(mixed $needle[, bool $strict = false ]) : string|int|false
Parameters
$needle : mixed
$strict : bool = false

Whether to compare with ===.

Return values
string|int|false

serialize()

Serialises the collection to a JSON string.

public serialize([int $flags = 0 ][, int|null $depth = 512 ]) : string
Parameters
$flags : int = 0

json_encode flags.

$depth : int|null = 512

json_encode depth.

Return values
string

set()

Sets a value in the collection at `$offset`.

public set(mixed $offset, mixed $value) : mixed
Parameters
$offset : mixed
$value : mixed

shift()

public shift() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null —

The [key => value] pair removed from the front, or null when the collection is empty.

slice()

Returns a new collection with the slice `$offset`..`$offset + $length`.

public slice(int $offset[, int|null $length = null ][, bool $preserve_keys = false ]) : ExCollectionInterface
Parameters
$offset : int
$length : int|null = null
$preserve_keys : bool = false
Return values
ExCollectionInterface

splice()

Removes `$length` items starting at `$offset`, inserting `$replacement` in their place. Mutates the collection and returns it.

public splice(int $offset, int|null $length[, mixed $replacement = [] ]) : ExCollectionInterface
Parameters
$offset : int
$length : int|null
$replacement : mixed = []
Return values
ExCollectionInterface

toArray()

public toArray([bool $assoc = true ]) : array<string|int, mixed>

Use jsonSerialize

Parameters
$assoc : bool = true

Whether to keep string keys.

Return values
array<string|int, mixed>

unserialize()

Restores the collection from a JSON string produced by {@see serialize()}.

public unserialize(string $serialized) : void
Parameters
$serialized : string

values()

public values() : array<string|int, mixed>
Return values
array<string|int, mixed> —

The collection's values, re-indexed.

On this page

Search results