interface Collection implements Countable, IteratorAggregate, ArrayAccess (View source)

The missing (SPL) Collection/Array/OrderedMap interface.

A Collection resembles the nature of a regular PHP array. That is, it is essentially an ordered map that can also be used like a list.

A Collection has an internal iterator just like a PHP array. In addition, a Collection can be iterated with external iterators, which is preferrable. To use an external iterator simply use the foreach language construct to iterate over the collection (which calls {@link getIterator()} internally) or explicitly retrieve an iterator though {@link getIterator()} which can then be used to iterate over the collection. You can not rely on the internal iterator of the collection being at a certain position unless you explicitly positioned it before. Prefer iteration with external iterators.

Methods

boolean
add(mixed $element)

Adds an element at the end of the collection.

clear()

Clears the collection, removing all elements.

boolean
contains(mixed $element)

Checks whether an element is contained in the collection.

boolean
isEmpty()

Checks whether the collection is empty (contains no elements).

mixed
remove(string|integer $key)

Removes the element at the specified index from the collection.

boolean
removeElement(mixed $element)

Removes the specified element from the collection, if it is found.

boolean
containsKey(string|integer $key)

Checks whether the collection contains an element with the specified key/index.

mixed
get(string|integer $key)

Gets the element at the specified key/index.

array
getKeys()

Gets all keys/indices of the collection.

array
getValues()

Gets all values of the collection.

set(string|integer $key, mixed $value)

Sets an element in the collection at the specified key/index.

array
toArray()

Gets a native PHP array representation of the collection.

mixed
first()

Sets the internal iterator to the first element in the collection and returns this element.

mixed
last()

Sets the internal iterator to the last element in the collection and returns this element.

key()

Gets the key/index of the element at the current iterator position.

current()

Gets the element of the collection at the current iterator position.

next()

Moves the internal iterator position to the next element.

boolean
exists(Closure $p)

Tests for the existence of an element that satisfies the given predicate.

filter(Closure $p)

Returns all the elements of this collection that satisfy the predicate p.

boolean
forAll(Closure $p)

Applies the given predicate p to all elements of this collection, returning true, if the predicate yields true for all elements.

map(Closure $func)

Applies the given function to each element in the collection and returns a new collection with the elements returned by the function.

array
partition(Closure $p)

Partitions this collection in two collections according to a predicate.

mixed
indexOf(mixed $element)

Gets the index/key of a given element. The comparison of two elements is strict, that means not only the value but also the type must match.

array
slice(int $offset, int $length = null)

Extract a slice of $length elements starting at position $offset from the Collection.

Details

boolean add(mixed $element)

Adds an element at the end of the collection.

Parameters

mixed $element The element to add.

Return Value

boolean Always TRUE.

clear()

Clears the collection, removing all elements.

boolean contains(mixed $element)

Checks whether an element is contained in the collection.

This is an O(n) operation, where n is the size of the collection.

Parameters

mixed $element The element to search for.

Return Value

boolean TRUE if the collection contains the element, FALSE otherwise.

boolean isEmpty()

Checks whether the collection is empty (contains no elements).

Return Value

boolean TRUE if the collection is empty, FALSE otherwise.

mixed remove(string|integer $key)

Removes the element at the specified index from the collection.

Parameters

string|integer $key The kex/index of the element to remove.

Return Value

mixed The removed element or NULL, if the collection did not contain the element.

boolean removeElement(mixed $element)

Removes the specified element from the collection, if it is found.

Parameters

mixed $element The element to remove.

Return Value

boolean TRUE if this collection contained the specified element, FALSE otherwise.

boolean containsKey(string|integer $key)

Checks whether the collection contains an element with the specified key/index.

Parameters

string|integer $key The key/index to check for.

Return Value

boolean TRUE if the collection contains an element with the specified key/index, FALSE otherwise.

mixed get(string|integer $key)

Gets the element at the specified key/index.

Parameters

string|integer $key The key/index of the element to retrieve.

Return Value

mixed

array getKeys()

Gets all keys/indices of the collection.

Return Value

array The keys/indices of the collection, in the order of the corresponding elements in the collection.

array getValues()

Gets all values of the collection.

Return Value

array The values of all elements in the collection, in the order they appear in the collection.

set(string|integer $key, mixed $value)

Sets an element in the collection at the specified key/index.

Parameters

string|integer $key The key/index of the element to set.
mixed $value The element to set.

array toArray()

Gets a native PHP array representation of the collection.

Return Value

array

mixed first()

Sets the internal iterator to the first element in the collection and returns this element.

Return Value

mixed

mixed last()

Sets the internal iterator to the last element in the collection and returns this element.

Return Value

mixed

key()

Gets the key/index of the element at the current iterator position.

current()

Gets the element of the collection at the current iterator position.

next()

Moves the internal iterator position to the next element.

boolean exists(Closure $p)

Tests for the existence of an element that satisfies the given predicate.

Parameters

Closure $p The predicate.

Return Value

boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise.

Collection filter(Closure $p)

Returns all the elements of this collection that satisfy the predicate p.

The order of the elements is preserved.

Parameters

Closure $p The predicate used for filtering.

Return Value

Collection A collection with the results of the filter operation.

boolean forAll(Closure $p)

Applies the given predicate p to all elements of this collection, returning true, if the predicate yields true for all elements.

Parameters

Closure $p The predicate.

Return Value

boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.

Collection map(Closure $func)

Applies the given function to each element in the collection and returns a new collection with the elements returned by the function.

Parameters

Closure $func

Return Value

Collection

array partition(Closure $p)

Partitions this collection in two collections according to a predicate.

Keys are preserved in the resulting collections.

Parameters

Closure $p The predicate on which to partition.

Return Value

array An array with two elements. The first element contains the collection of elements where the predicate returned TRUE, the second element contains the collection of elements where the predicate returned FALSE.

mixed indexOf(mixed $element)

Gets the index/key of a given element. The comparison of two elements is strict, that means not only the value but also the type must match.

For objects this means reference equality.

Parameters

mixed $element The element to search for.

Return Value

mixed The key/index of the element or FALSE if the element was not found.

array slice(int $offset, int $length = null)

Extract a slice of $length elements starting at position $offset from the Collection.

If $length is null it returns all elements from $offset to the end of the Collection. Keys have to be preserved by this method. Calling this method will only return the selected slice and NOT change the elements contained in the collection slice is called on.

Parameters

int $offset
int $length

Return Value

array