Documentation

CollectionInterface extends ArrayInterface

A collection represents a group of objects, known as its elements.

Some collections allow duplicate elements and others do not. Some are ordered and others unordered.

Tags
template

T

extends

ArrayInterface<T>

Table of Contents

SORT_ASC  = 'asc'
Ascending sort type.
SORT_DESC  = 'desc'
Descending sort type.
add()  : bool
Ensures that this collection contains the specified element (optional operation).
clear()  : void
Removes all items from this array.
column()  : array<int, mixed>
Returns the values from the given property or method.
contains()  : bool
Returns `true` if this collection contains the specified element.
diff()  : CollectionInterface<string|int, T>
Create a new collection with divergent items between current and given collection.
filter()  : CollectionInterface<string|int, T>
Filter out items of the collection which don't match the criteria of given callback.
first()  : T
Returns the first item of the collection.
getType()  : string
Returns the type associated with this collection.
intersect()  : CollectionInterface<string|int, T>
Create a new collection with intersecting item between current and given collection.
isEmpty()  : bool
Returns `true` if this array is empty.
last()  : T
Returns the last item of the collection.
map()  : CollectionInterface<string|int, TCallbackReturn>
Apply a given callback method on each item of the collection.
merge()  : CollectionInterface<string|int, T>
Merge current items and items of given collections into a new one.
remove()  : bool
Removes a single instance of the specified element from this collection, if it is present.
sort()  : CollectionInterface<string|int, T>
Sort the collection by a property or method with the given sort order.
toArray()  : array<array-key, T>
Returns a native PHP array representation of this array object.
where()  : CollectionInterface<string|int, T>
Create a new collection where items match the criteria of given callback.

Constants

Methods

add()

Ensures that this collection contains the specified element (optional operation).

public add(T $element) : bool

Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)

Collections that support this operation may place limitations on what elements may be added to this collection. In particular, some collections will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. Collection classes should clearly specify in their documentation any restrictions on what elements may be added.

If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false). This preserves the invariant that a collection always contains the specified element after this call returns.

Parameters
$element : T

The element to add to the collection.

Return values
bool

true if this collection changed as a result of the call.

clear()

Removes all items from this array.

public clear() : void
Return values
void

column()

Returns the values from the given property or method.

public column(string $propertyOrMethod) : array<int, mixed>
Parameters
$propertyOrMethod : string

The property or method name to filter by.

Return values
array<int, mixed>

contains()

Returns `true` if this collection contains the specified element.

public contains(T $element[, bool $strict = true ]) : bool
Parameters
$element : T

The element to check whether the collection contains.

$strict : bool = true

Whether to perform a strict type check on the value.

Return values
bool

first()

Returns the first item of the collection.

public first() : T
Return values
T

getType()

Returns the type associated with this collection.

public getType() : string
Return values
string

isEmpty()

Returns `true` if this array is empty.

public isEmpty() : bool
Return values
bool

last()

Returns the last item of the collection.

public last() : T
Return values
T

map()

Apply a given callback method on each item of the collection.

public map(callable $callback) : CollectionInterface<string|int, TCallbackReturn>

This will always leave the original collection untouched. The new collection is created by mapping the callback to each item of the original collection.

See the PHP array_map() documentation for examples of how the $callback parameter works.

Parameters
$callback : callable
Tags
template

TCallbackReturn

Return values
CollectionInterface<string|int, TCallbackReturn>

remove()

Removes a single instance of the specified element from this collection, if it is present.

public remove(T $element) : bool
Parameters
$element : T

The element to remove from the collection.

Return values
bool

true if an element was removed as a result of this call.

sort()

Sort the collection by a property or method with the given sort order.

public sort(string $propertyOrMethod[, string $order = self::SORT_ASC ]) : CollectionInterface<string|int, T>

This will always leave the original collection untouched and will return a new one.

Parameters
$propertyOrMethod : string

The property or method to sort by.

$order : string = self::SORT_ASC

The sort order for the resulting collection (one of this interface's SORT_* constants).

Return values
CollectionInterface<string|int, T>

toArray()

Returns a native PHP array representation of this array object.

public toArray() : array<array-key, T>
Return values
array<array-key, T>

where()

Create a new collection where items match the criteria of given callback.

public where(string $propertyOrMethod, mixed $value) : CollectionInterface<string|int, T>

This will always leave the original collection untouched and will return a new one.

Parameters
$propertyOrMethod : string

The property or method to evaluate.

$value : mixed

The value to match.

Return values
CollectionInterface<string|int, T>

Search results