class ArrayCollection implements Collection, Selectable (View source)

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

Methods

__construct(array $elements = array())

Initializes a new ArrayCollection.

array
toArray()

Gets the PHP array representation of this 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 current key/index at the current internal iterator position.

next()

Moves the internal iterator position to the next element.

current()

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

mixed
remove(string|integer $key)

Removes an element with a specific key/index from the collection.

boolean
removeElement(mixed $element)

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

bool
offsetExists(mixed $offset)

ArrayAccess implementation of offsetExists()

mixed
offsetGet(mixed $offset)

ArrayAccess implementation of offsetGet()

bool
offsetSet(mixed $offset, mixed $value)

ArrayAccess implementation of offsetSet()

mixed
offsetUnset(mixed $offset)

ArrayAccess implementation of offsetUnset()

boolean
containsKey(string|integer $key)

Checks whether the collection contains a specific key/index.

boolean
contains(mixed $element)

Checks whether the given element is contained in the collection.

boolean
exists(Closure $p)

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

mixed
indexOf(mixed $element)

Searches for a given element and, if found, returns the corresponding key/index of that element. The comparison of two elements is strict, that means not only the value but also the type must match.

mixed
get(string|integer $key)

Gets the element with the given key/index.

array
getKeys()

Gets all keys/indexes of the collection elements.

array
getValues()

Gets all elements.

integer
count()

Returns the number of elements in the collection.

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

Adds/sets an element in the collection at the index / with the specified key.

boolean
add(mixed $value)

Adds an element to the collection.

boolean
isEmpty()

Checks whether the collection is empty.

getIterator()

Gets an iterator for iterating over the elements in the 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.

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.

array
partition(Closure $p)

Partitions this collection in two collections according to a predicate.

string
__toString()

Returns a string representation of this object.

clear()

Clears the collection.

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

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

matching(Criteria $criteria)

Select all elements from a selectable that match the criteria and return a new collection containing these elements.

Details

__construct(array $elements = array())

Initializes a new ArrayCollection.

Parameters

array $elements

array toArray()

Gets the PHP array representation of this 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 current key/index at the current internal iterator position.

next()

Moves the internal iterator position to the next element.

current()

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

mixed remove(string|integer $key)

Removes an element with a specific key/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.

bool offsetExists(mixed $offset)

ArrayAccess implementation of offsetExists()

Parameters

mixed $offset

Return Value

bool

See also

containsKey()

mixed offsetGet(mixed $offset)

ArrayAccess implementation of offsetGet()

Parameters

mixed $offset

Return Value

mixed

See also

get()

bool offsetSet(mixed $offset, mixed $value)

ArrayAccess implementation of offsetSet()

Parameters

mixed $offset
mixed $value

Return Value

bool

See also

add()
set()

mixed offsetUnset(mixed $offset)

ArrayAccess implementation of offsetUnset()

Parameters

mixed $offset

Return Value

mixed

See also

remove()

boolean containsKey(string|integer $key)

Checks whether the collection contains a specific 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.

boolean contains(mixed $element)

Checks whether the given element is contained in the collection.

Only element values are compared, not keys. 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

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

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.

mixed indexOf(mixed $element)

Searches for a given element and, if found, returns the corresponding key/index of that 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.

mixed get(string|integer $key)

Gets the element with the given key/index.

Parameters

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

Return Value

mixed

array getKeys()

Gets all keys/indexes of the collection elements.

Return Value

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

array getValues()

Gets all elements.

Return Value

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

integer count()

Returns the number of elements in the collection.

Implementation of the Countable interface.

Return Value

integer The number of elements in the collection.

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

Adds/sets an element in the collection at the index / with the specified key.

When the collection is a Map this is like put(key,value)/add(key,value). When the collection is a List this is like add(position,value).

Parameters

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

boolean add(mixed $value)

Adds an element to the collection.

Parameters

mixed $value

Return Value

boolean Always TRUE.

boolean isEmpty()

Checks whether the collection is empty.

Note: This is preferable over count() == 0.

Return Value

boolean TRUE if the collection is empty, FALSE otherwise.

ArrayIterator getIterator()

Gets an iterator for iterating over the elements in the collection.

Return Value

ArrayIterator

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.

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.

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.

string __toString()

Returns a string representation of this object.

Return Value

string

clear()

Clears the collection.

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

Collection matching(Criteria $criteria)

Select all elements from a selectable that match the criteria and return a new collection containing these elements.

Parameters

Criteria $criteria

Return Value

Collection