Collection Documentation

CollectionInterface extends ArrayAccess, JsonSerializable, IteratorAggregate, Countable

Contract for a keyed collection of items. The reference implementation is {@see CollectionTrait}; see {@see ExCollectionInterface} for the extended (array-helper) surface.

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()`.
clear()  : void
Removes every item from the collection.
count()  : int
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.
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`.
isset()  : bool
Whether an item exists at `$offset`.
jsonSerialize()  : 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.
serialize()  : string
Serialises the collection to a JSON string.
set()  : mixed
Sets a value in the collection at `$offset`.
toArray()  : array<string|int, mixed>
unserialize()  : void
Restores the collection from a JSON string produced by {@see serialize()}.

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>

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

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>

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

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

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
On this page

Search results