class ArrayCollection implements Collection, Selectable (View source)

An ArrayCollection is a Collection implementation that wraps a regular PHP array.

Warning: Using (un-)serialize() on a collection is not a supported use-case and may break when we change the internals in the future. If you need to serialize a collection use {@link toArray()} and reconstruct the collection manually.

Methods

__construct(array $elements = [])

Initializes a new ArrayCollection.

createFrom(array $elements)

Creates a new instance from the specified elements.

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.

int|string
key()

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

mixed
next()

Moves the internal iterator position to the next element and returns this element.

mixed
current()

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

mixed
remove(string|int $key)

Removes the element at the specified index from the collection.

bool
removeElement(mixed $element)

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

offsetExists($offset)

Required by interface ArrayAccess.

offsetGet($offset)

Required by interface ArrayAccess.

offsetSet($offset, $value)

Required by interface ArrayAccess.

offsetUnset($offset)

Required by interface ArrayAccess.

bool
containsKey(string|int $key)

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

bool
contains(mixed $element)

Checks whether an element is contained in the collection.

bool
exists(Closure $p)

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

int|string|bool
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.

mixed
get(string|int $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.

count()

{@inheritDoc}

void
set(string|int $key, mixed $value)

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

bool
add(mixed $element)

Adds an element at the end of the collection.

bool
isEmpty()

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

getIterator()

Required by interface IteratorAggregate.

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.

filter(Closure $p)

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

bool
forAll(Closure $p)

Tests whether the given predicate p holds for all elements of this collection.

partition(Closure $p)

Partitions this collection in two collections according to a predicate.

string
__toString()

Returns a string representation of this object.

void
clear()

Clears the collection, removing all elements.

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

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

matching(Criteria $criteria)

Selects all elements from a selectable that match the expression and returns a new collection containing these elements.

Details

__construct(array $elements = [])

Initializes a new ArrayCollection.

Parameters

array $elements

protected ArrayCollection createFrom(array $elements)

Creates a new instance from the specified elements.

This method is provided for derived classes to specify how a new instance should be created when constructor semantics have changed.

Parameters

array $elements Elements.

Return Value

ArrayCollection

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

int|string key()

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

Return Value

int|string

mixed next()

Moves the internal iterator position to the next element and returns this element.

Return Value

mixed

mixed current()

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

Return Value

mixed

mixed remove(string|int $key)

Removes the element at the specified index from the collection.

Parameters

string|int $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.

bool removeElement(mixed $element)

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

Parameters

mixed $element The element to remove.

Return Value

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

offsetExists($offset)

Required by interface ArrayAccess.

{@inheritDoc}

Parameters

$offset

offsetGet($offset)

Required by interface ArrayAccess.

{@inheritDoc}

Parameters

$offset

offsetSet($offset, $value)

Required by interface ArrayAccess.

{@inheritDoc}

Parameters

$offset
$value

offsetUnset($offset)

Required by interface ArrayAccess.

{@inheritDoc}

Parameters

$offset

bool containsKey(string|int $key)

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

Parameters

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

Return Value

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

bool 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

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

bool exists(Closure $p)

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

Parameters

Closure $p The predicate.

Return Value

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

int|string|bool 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

int|string|bool The key/index of the element or FALSE if the element was not found.

mixed get(string|int $key)

Gets the element at the specified key/index.

Parameters

string|int $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.

count()

{@inheritDoc}

void set(string|int $key, mixed $value)

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

Parameters

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

Return Value

void

bool add(mixed $element)

Adds an element at the end of the collection.

Parameters

mixed $element The element to add.

Return Value

bool Always TRUE.

bool isEmpty()

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

Return Value

bool TRUE if the collection is empty, FALSE otherwise.

getIterator()

Required by interface IteratorAggregate.

{@inheritDoc}

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

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.

bool forAll(Closure $p)

Tests whether the given predicate p holds for all elements of this collection.

Parameters

Closure $p The predicate.

Return Value

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

Collection[] 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

Collection[] 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.

string __toString()

Returns a string representation of this object.

Return Value

string

void clear()

Clears the collection, removing all elements.

Return Value

void

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

Extracts 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 The offset to start from.
int|null $length The maximum number of elements to return, or null for no limit.

Return Value

array

Collection matching(Criteria $criteria)

Selects all elements from a selectable that match the expression and returns a new collection containing these elements.

Parameters

Criteria $criteria

Return Value

Collection