Collection
class Collection implements ArrayAccess, ArrayableInterface, Countable, IteratorAggregate, JsonableInterface, JsonSerializable (View source)
Properties
protected array | $items | The items contained in the collection. |
Methods
void | __construct(array $items = array()) Create a new collection. | |
static Collection | make(mixed $items) Create a new collection instance if the value isn't one already. | |
array | all() Get all of the items in the collection. | |
Collection | collapse() Collapse the collection items into a single array. | |
bool | contains(mixed $value) Determine if an item exists in the collection. | |
Collection | diff(Collection|ArrayableInterface|array $items) Diff the collection with the given items. | |
$this | each(Closure $callback) Execute a callback over each item. | |
Collection | fetch(string $key) Fetch a nested element of the collection. | |
Collection | filter(Closure $callback) Run a filter over each of the items. | |
mixed|null | first(Closure $callback = null, mixed $default = null) Get the first item from the collection. | |
Collection | flatten() Get a flattened array of the items in the collection. | |
Collection | flip() Flip the items in the collection. | |
void | forget(mixed $key) Remove an item from the collection by key. | |
mixed | get(mixed $key, mixed $default = null) Get an item from the collection by key. | |
Collection | groupBy(callable|string $groupBy) Group an associative array by a field or Closure value. | |
string | getGroupByKey(callable|string $groupBy, string $key, mixed $value) Get the "group by" key value. | |
Collection | keyBy(string $keyBy) Key an associative array by a field. | |
bool | has(mixed $key) Determine if an item exists in the collection by key. | |
string | implode(string $value, string $glue = null) Concatenate values of a given key as a string. | |
Collection | intersect(Collection|ArrayableInterface|array $items) Intersect the collection with the given items. | |
bool | isEmpty() Determine if the collection is empty or not. | |
array | keys() Get the keys of the collection items. | |
mixed|null | last() Get the last item from the collection. | |
array | lists(string $value, string $key = null) Get an array with the values of a given key. | |
Collection | map(Closure $callback) Run a map over each of the items. | |
Collection | merge(Collection|ArrayableInterface|array $items) Merge the collection with the given items. | |
mixed|null | pop() Get and remove the last item from the collection. | |
void | prepend(mixed $value) Push an item onto the beginning of the collection. | |
void | push(mixed $value) Push an item onto the end of the collection. | |
mixed | pull(mixed $key, mixed $default = null) Pulls an item from the collection. | |
void | put(mixed $key, mixed $value) Put an item in the collection by key. | |
mixed | random(int $amount = 1) Get one or more items randomly from the collection. | |
mixed | reduce(callable $callback, mixed $initial = null) Reduce the collection to a single value. | |
Collection | reject(Closure|mixed $callback) Create a collection of all elements that do not pass a given truth test. | |
Collection | reverse() Reverse items order. | |
mixed | search(mixed $value, bool $strict = false) Search the collection for a given value and return the corresponding key if successful. | |
mixed|null | shift() Get and remove the first item from the collection. | |
$this | shuffle() Shuffle the items in the collection. | |
Collection | slice(int $offset, int $length = null, bool $preserveKeys = false) Slice the underlying collection array. | |
Collection | chunk(int $size, bool $preserveKeys = false) Chunk the underlying collection array. | |
$this | sort(Closure $callback) Sort through each item with a callback. | |
$this | sortBy(Closure|string $callback, int $options = SORT_REGULAR, bool $descending = false) Sort the collection using the given Closure. | |
$this | sortByDesc(Closure|string $callback, int $options = SORT_REGULAR) Sort the collection in descending order using the given Closure. | |
Collection | splice(int $offset, int $length, mixed $replacement = array()) Splice portion of the underlying collection array. | |
mixed | sum(Closure $callback = null) Get the sum of the given values. | |
Collection | take(int $limit = null) Take the first or last {$limit} items. | |
$this | transform(Closure $callback) Transform each item in the collection using a callback. | |
Collection | unique() Return only unique items from the collection array. | |
Collection | values() Reset the keys on the underlying array. | |
Closure | valueRetriever(string $value) Get a value retrieving callback. | |
array | toArray() Get the collection of items as a plain array. | |
array | jsonSerialize() Convert the object into something JSON serializable. | |
string | toJson(int $options) Get the collection of items as JSON. | |
ArrayIterator | getIterator() Get an iterator for the items. | |
CachingIterator | getCachingIterator(int $flags = CachingIterator::CALL_TOSTRING) Get a CachingIterator instance. | |
int | count() Count the number of items in the collection. | |
bool | offsetExists(mixed $key) Determine if an item exists at an offset. | |
mixed | offsetGet(mixed $key) Get an item at a given offset. | |
void | offsetSet(mixed $key, mixed $value) Set the item at a given offset. | |
void | offsetUnset(string $key) Unset the item at a given offset. | |
string | __toString() Convert the collection to its string representation. | |
array | getArrayableItems(Collection|ArrayableInterface|array $items) Results array of items from Collection or ArrayableInterface. |
Details
void __construct(array $items = array())
Create a new collection.
static Collection make(mixed $items)
Create a new collection instance if the value isn't one already.
array all()
Get all of the items in the collection.
Collection collapse()
Collapse the collection items into a single array.
bool contains(mixed $value)
Determine if an item exists in the collection.
Collection diff(Collection|ArrayableInterface|array $items)
Diff the collection with the given items.
$this each(Closure $callback)
Execute a callback over each item.
Collection fetch(string $key)
Fetch a nested element of the collection.
Collection filter(Closure $callback)
Run a filter over each of the items.
mixed|null first(Closure $callback = null, mixed $default = null)
Get the first item from the collection.
Collection flatten()
Get a flattened array of the items in the collection.
Collection flip()
Flip the items in the collection.
void forget(mixed $key)
Remove an item from the collection by key.
mixed get(mixed $key, mixed $default = null)
Get an item from the collection by key.
Collection groupBy(callable|string $groupBy)
Group an associative array by a field or Closure value.
protected string getGroupByKey(callable|string $groupBy, string $key, mixed $value)
Get the "group by" key value.
Collection keyBy(string $keyBy)
Key an associative array by a field.
bool has(mixed $key)
Determine if an item exists in the collection by key.
string implode(string $value, string $glue = null)
Concatenate values of a given key as a string.
Collection intersect(Collection|ArrayableInterface|array $items)
Intersect the collection with the given items.
bool isEmpty()
Determine if the collection is empty or not.
array keys()
Get the keys of the collection items.
mixed|null last()
Get the last item from the collection.
array lists(string $value, string $key = null)
Get an array with the values of a given key.
Collection map(Closure $callback)
Run a map over each of the items.
Collection merge(Collection|ArrayableInterface|array $items)
Merge the collection with the given items.
mixed|null pop()
Get and remove the last item from the collection.
void prepend(mixed $value)
Push an item onto the beginning of the collection.
void push(mixed $value)
Push an item onto the end of the collection.
mixed pull(mixed $key, mixed $default = null)
Pulls an item from the collection.
void put(mixed $key, mixed $value)
Put an item in the collection by key.
mixed random(int $amount = 1)
Get one or more items randomly from the collection.
mixed reduce(callable $callback, mixed $initial = null)
Reduce the collection to a single value.
Collection reject(Closure|mixed $callback)
Create a collection of all elements that do not pass a given truth test.
Collection reverse()
Reverse items order.
mixed search(mixed $value, bool $strict = false)
Search the collection for a given value and return the corresponding key if successful.
mixed|null shift()
Get and remove the first item from the collection.
$this shuffle()
Shuffle the items in the collection.
Collection slice(int $offset, int $length = null, bool $preserveKeys = false)
Slice the underlying collection array.
Collection chunk(int $size, bool $preserveKeys = false)
Chunk the underlying collection array.
$this sort(Closure $callback)
Sort through each item with a callback.
$this sortBy(Closure|string $callback, int $options = SORT_REGULAR, bool $descending = false)
Sort the collection using the given Closure.
$this sortByDesc(Closure|string $callback, int $options = SORT_REGULAR)
Sort the collection in descending order using the given Closure.
Collection splice(int $offset, int $length, mixed $replacement = array())
Splice portion of the underlying collection array.
mixed sum(Closure $callback = null)
Get the sum of the given values.
Collection take(int $limit = null)
Take the first or last {$limit} items.
$this transform(Closure $callback)
Transform each item in the collection using a callback.
Collection unique()
Return only unique items from the collection array.
Collection values()
Reset the keys on the underlying array.
protected Closure valueRetriever(string $value)
Get a value retrieving callback.
array toArray()
Get the collection of items as a plain array.
array jsonSerialize()
Convert the object into something JSON serializable.
string toJson(int $options)
Get the collection of items as JSON.
ArrayIterator getIterator()
Get an iterator for the items.
CachingIterator getCachingIterator(int $flags = CachingIterator::CALL_TOSTRING)
Get a CachingIterator instance.
int count()
Count the number of items in the collection.
bool offsetExists(mixed $key)
Determine if an item exists at an offset.
mixed offsetGet(mixed $key)
Get an item at a given offset.
void offsetSet(mixed $key, mixed $value)
Set the item at a given offset.
void offsetUnset(string $key)
Unset the item at a given offset.
string __toString()
Convert the collection to its string representation.
protected array getArrayableItems(Collection|ArrayableInterface|array $items)
Results array of items from Collection or ArrayableInterface.
© Taylor Otwell
Licensed under the MIT License.
Laravel is a trademark of Taylor Otwell.
https://laravel.com/api/4.2/Illuminate/Support/Collection.html