Doctrine Collections is a library that contains classes for working with
arrays of data. Here is an example using the simple
Doctrine Collections provides an interface named
that 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 preferable.
To use an external iterator simply use the foreach language construct to
iterate over the collection, which calls
getIterator() internally, or
explicitly retrieve an iterator though
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.
The methods available on the interface are:
Adds an element at the end of the collection.
Clears the collection, removing all elements.
Checks whether an element is contained in the collection. This is an O(n) operation, where n is the size of the collection.
Checks whether the collection contains an element with the specified key/index.
Gets the element of the collection at the current iterator position.
Gets the element at the specified key/index.
Gets all keys/indices of the collection.
Gets all values of the collection.
Checks whether the collection is empty (contains no elements).
Sets the internal iterator to the first element in the collection and returns this element.
Tests for the existence of an element that satisfies the given predicate.
Returns all the elements of this collection that satisfy the predicate. The order of the elements is preserved.
Tests whether the given predicate holds for all elements of this collection.
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.
Gets the key/index of the element at the current iterator position.
Sets the internal iterator to the last element in the collection and returns this element.
Applies the given function to each element in the collection and returns a new collection with the elements returned by the function.
Selects all elements from a selectable that match the expression and returns a new collection containing these elements.
1 use Doctrine\Common\Collections\Criteria; use Doctrine\Common\Collections\Expr\Comparison; $collection = new ArrayCollection([ [ 'name' => 'jwage', ], [ 'name' => 'romanb', ], ]); $expr = new Comparison('name', '=', 'jwage'); $criteria = new Criteria(); $criteria->where($expr); $matched = $collection->matching($criteria); // ['jwage'] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
You can read more about expressions here.
Moves the internal iterator position to the next element and returns this element.
Partitions this collection in two collections according to a predicate. Keys are preserved in the resulting collections.
Removes the element at the specified index from the collection.
Removes the specified element from the collection, if it is found.
Sets an element in the collection at the specified key/index.
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.
Gets a native PHP array representation of the collection.