Legacy Documentation
You are using the documentation for version 3.5.17. Go here for the latest version or check here for your available upgrades to the latest version.
Collections¶
Simple Example¶
The collection class is a list-style container for composite datatypes that adds more utility than storing them in a simple PHP array. It can store both arrays and objects:
use EllisLab\ExpressionEngine\Library\Data\Collection;
$collection = new Collection(array(
array('name' => 'Anne', 'age' => 47),
array('name' => 'Bob', 'age' => 32),
array('name' => 'Julie', 'age' => 14),
array('name' => 'Jack', 'age' => 86)
));
$collection->count(); // 4
$collection->first(); // Anne's array
$sorted = $collection->sortBy('age');
$sorted->first(); // Julie's array
For basic interactions a collection can be treated as a regular array:
foreach ($collection as $person)
{
echo $person['age']; // or $person->age if your collection contains objects
}
$collection[] = array('name' => 'Savannah', 27);
Note
Indices are always uninterrupted numeric sequences starting at 0. Do not use string keys.
Method Reference¶
-
EllisLab\ExpressionEngine\Library\Data\Collection
-
asArray
()¶ Turn the collection into an array
Returns: The collection items Return type: Array
-
first
()¶ Get the first element in the collection
Returns: The first element in the collection (or NULL if empty) Return type: Mixed
-
last
()¶ Get the last element in the collection
Returns: The last element in the collection (or NULL if empty) Return type: Mixed
-
pluck
($key)¶ Extract a value from all elements of the collection
Parameters: - $key (string) – The key or property name to pluck
Returns: Array of values for the key
Return type: Array
-
collect
($collector)¶ Extract a value from all elements of the collection using a keyname or callback.
Parameters: - $extractor (string|Closure) – The name of the property or a closure that returns a value for an item.
Returns: Array of values for the key
Return type: Array
-
sortBy
($column, $flags = SORT_REGULAR)¶ Sort a collection by a certain element key. Returns a new collection.
Parameters: - $key (string) – The key or property to sort by
- $flags (int) – A PHP sort flag
Returns: A new collection
Return type: Collection
-
reverse
()¶ Reverse the elements in the collection. Returns a new collection.
Returns: A new collection with the elements in reverse Return type: Collection
-
indexBy
($extractor)¶ Return an associative array of all items indexed by a given element.
It is up to you to ensure that the index keys are unique. If
$extractor
is a closure it will be passed each element in the collection and should return the value to use.Parameters: - $extractor (string|Closure) – The name of the property or a closure that returns a value for an item.
Returns: Associative array of elements
Return type: Array
-
getDictionary
($key, $value)¶ Return a key-value array composed of two items in each collection element.
It is up to you to ensure that the index keys are unique.
Parameters: - $extractor (string|Closure) – The name of the property or a closure that returns a value for an item.
Returns: Associative key-value array
Return type: Array
-
map
($callback)¶ Applies the given callback to the collection and returns an array of the results.
Parameters: - $key (string) – The name of the property
Returns: The value of the property
Return type: Array
-
filter
($callback)¶ Filter elements of a collection using a callback function. If the callback returns
TRUE
the current value from the collection is returned in the result Collection.Parameters: - $callback (Closure) – The callback function to use
Returns: New collection of filtered elements.
Return type: Collection
-
each
($callback)¶ Iterates over all the elements in the Collection and passes them to them to the Callback one at a time.
Parameters: - $callback (Closure) – The callback to pass each element to
Returns: The original collection
Return type: Collection
-
count
()¶ Count the elements in the Collection
Returns: The number of items in the Collection Return type: int