Download the PHP package yii2mod/collection without Composer
On this page you can find all versions of the php package yii2mod/collection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download yii2mod/collection
More information about yii2mod/collection
Files in yii2mod/collection
Informations about the package collection
Collection Extension for Yii 2
The yii2mod\collection\Collection
class provides a fluent, convenient wrapper for working with arrays of data.
Support us
Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the require section of your composer.json
file.
Creating Collections
Available Methods
- all()
- avg()
- chunk()
- collapse()
- combine()
- contains()
- count()
- diff()
- each()
- every()
- except()
- filter()
- first()
- last()
- flatten()
- flip()
- forget()
- forPage()
- get()
- groupBy()
- has()
- implode()
- intersect()
- isEmpty()
- isNotEmpty()
- keyby()
- keys()
- map()
- max()
- merge()
- min()
- only()
- pluck()
- pop()
- prepend()
- pull()
- push()
- put()
- random()
- reduce()
- reject()
- reverse()
- search()
- shift()
- shuffle()
- slice()
- sort()
- sortBy()
- sortByDesc()
- splice()
- sum()
- take()
- toArray()
- tap()
- toJson()
- transform()
- unique()
- uniqueStrict()
- values()
- where()
- whereLoose()
- whereIn()
- whereInLoose()
- zip()
Method Listing
The all
method simply returns the underlying array represented by the collection:
The avg
method returns the average of all items in the collection:
If the collection contains nested arrays or objects, you should pass a key to use for determining which values to calculate the average:
The chunk
method breaks the collection into multiple, smaller collections of a given size:
The collapse
method collapses a collection of arrays into a flat collection:
Create a collection by using this collection for keys and another for its values:
The contains
method determines whether the collection contains a given item:
You may also pass a key / value pair to the contains method, which will determine if the given pair exists in the collection:
The count
method returns the total number of items in the collection:
The diff
method compares the collection against another collection or a plain PHP array:
The each
method iterates over the items in the collection and passes each item to a given callback:
The every
method creates a new collection consisting of every n-th element:
You may optionally pass offset as the second argument:
Get all items except for those with the specified keys:
For the inverse of except
, see the only method.
The filter
method filters the collection by a given callback, keeping only those items that pass a given truth test:
The first
method returns the first element in the collection that passes a given truth test:
You may also call the first method with no arguments to get the first element in the collection. If the collection is empty, null
is returned:
The last
method returns the last element in the collection that passes a given truth test:
You may also call the last
method with no arguments to get the last element in the collection. If the collection is empty, null
is returned:
The flatten
method flattens a multi-dimensional collection into a single dimension:
The flip
method swaps the collection's keys with their corresponding values:
The forget
method removes an item from the collection by its key:
Unlike most other collection methods,
forget
does not return a new modified collection; it modifies the collection it is called on.
The forPage
method returns a new collection containing the items that would be present on a given page number:
The method requires the page number and the number of items to show per page, respectively.
Get an item from the collection by key:
You may optionally pass a default value as the second argument:
The groupBy
method groups the collection's items by a given key:
In addition to passing a string key, you may also pass a callback. The callback should return the value you wish to key the group by:
The has
method determines if a given key exists in the collection:
Concatenate values of a given key as a string:
If the collection contains simple strings or numeric values, simply pass the "glue" as the only argument to the method:
The intersect
method removes any values that are not present in the given array or collection:
The isEmpty
method returns true if the collection is empty; otherwise, false is returned:
The isNotEmpty
method returns true if the collection is not empty; otherwise, false is returned:
Key an associative array by a field or using a callback:
You may also pass your own callback, which should return the value to key the collection by:
The keys
method returns all of the collection's keys:
The map
method iterates through the collection and passes each value to the given callback. The callback is free to modify the item and return it, thus forming a new collection of modified items:
Like most other collection methods,
map
returns a new collection instance; it does not modify the collection it is called on. If you want to transform the original collection, use the transform method.
Get the max value of a given key:
Merge the collection with the given items:
Get the min value of a given key:
The only
method returns the items in the collection with the specified keys:
The pluck
method retrieves all of the collection values for a given key:
You may also specify how you wish the resulting collection to be keyed:
The pop
method removes and returns the last item from the collection:
The prepend
method adds an item to the beginning of the collection:
You can optionally pass a second argument to set the key of the prepended item:
The pull
method removes and returns an item from the collection by its key:
Push an item onto the end of the collection:
Put an item in the collection by key:
The random
method returns a random item from the collection:
You may optionally pass an integer to random. If that integer is more than 1, a collection of items is returned:
The reduce
method reduces the collection to a single value, passing the result of each iteration into the subsequent iteration:
The value for $carry on the first iteration is null; however, you may specify its initial value by passing a second argument to reduce:
The reject
method filters the collection using the given callback. The callback should return true for any items it wishes to remove from the resulting collection:
The reverse
method reverses the order of the collection's items:
Search the collection for a given value and return the corresponding key if successful:
The search is done using a "loose" comparison. To use strict comparison, pass true as the second argument to the method:
The shift
method removes and returns the first item from the collection:
Shuffle the items in the collection:
Slice the underlying collection array:
If you would like to limit the size of the returned slice, pass the desired size as the second argument to the method:
Sort through each item with a callback:
Sort the collection using the given callback:
You can also pass your own callback to determine how to sort the collection values:
This method has the same signature as the sortBy() method, but will sort the collection in the opposite order.
Splice a portion of the underlying collection array:
You may pass a second argument to limit the size of the resulting chunk:
In addition, you can pass a third argument containing the new items to replace the items removed from the collection:
Get the sum of the given values:
If the collection contains nested arrays or objects, you should pass a key to use for determining which values to sum:
In addition, you may pass your own callback to determine which values of the collection to sum:
Take the first or last {$limit} items:
You may also pass a negative integer to take the specified amount of items from the end of the collection:
Get the collection of items as a plain array:
all() method instead.
The tap
method passes the collection to the given callback, allowing you to "tap" into the collection at a specific point and do something with the items while not affecting the collection itself:
Get the collection of items as JSON:
Transform each item in the collection using a callback:
Unlike most other collection methods, map() method.
Return only unique items from the collection array:
The returned collection keeps the original array keys. In this example we used the values() method to reset the keys to consecutively numbered indexes.
When dealing with nested arrays or objects, you may specify the key used to determine uniqueness:
You may also pass your own callback to determine item uniqueness:
This method has the same signature as the unique
method; however, all values are compared using "strict" comparisons.
Reset the keys on the underlying array:
The where
method filters the collection by a given key / value pair:
The whereLoose() comparisons.
This method has the same signature as the where() method; however, all values are compared using "loose" comparisons.
The whereIn
method filters the collection by a given key / value contained within the given array.
The whereInLoose() method to filter using "loose" comparisons.
This method has the same signature as the whereIn() method; however, all values are compared using "loose" comparisons.
The zip
method merges together the values of the given array with the values of the collection at the corresponding index: