Download the PHP package mleczek/collections without Composer
On this page you can find all versions of the php package mleczek/collections. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mleczek/collections
More information about mleczek/collections
Files in mleczek/collections
Package collections
Short Description Easy way to manipulate arrays in PHP.
License MIT
Homepage https://github.com/mleczek/collections
Informations about the package collections
PHP Collections
This package was created to provide simple way to manipulate arrays in PHP. The package was inspired by the Laravel Collections.
Installation
Getting started
Convert any array to collection:
You can also do this using collection
helper method:
Available operations
- addFirst
- addLast
- avg
- chunk
- count
- each
- firstKey
- first
- flatten
- groupBy
- has
- isAssociative
- isEmpty
- isIndexed
- isNotEmpty
- join
- keyBy
- keys
- lastKey
- last
- map
- max
- merge
- min
- randomKey
- random
- reduce
- removeFirst
- removeLast
- reverse
- skip
- sortDesc
- sort
- sum
- take
- toArray
- unique
- values
- whereIn
- where
addFirst
Add item at the beginning of the collection.
addLast
Add item at the end of the collection.
avg
Calculate average value from items. Zero if there're no items in collection.
Throws when trying to calculate avg from non-number value.
chunk
Breaks into multiple, smaller collections of a given size.
E.g. for chunk size 2 the output array for [1, 2, 3]
will be [[1, 2], [3]]
.
count
The total number of items in the collection.
each
Iterate over each item.
firstKey
Get first item key.
first
Get first item value.
flatten
Convert array of arrays to array (remove one dimension).
groupBy
Groups the collection's items by a given key.
has
Check whether collection has items which match given closure.
isAssociative
Check whether collection is an associative array.
See also isIndexed
method.
isEmpty
Check whether collection has zero items.
isIndexed
Check whether collection is an indexed array.
See also isAssociative
method.
isNotEmpty
Check whether collection has any item.
join
Join all items with given glue.
keyBy
Change collection's items keys.
If multiple items have the same key, the exception will be thrown.
keys
Returns all of the collection's keys.
lastKey
Get last item key.
last
Get last item value.
map
Iterates through the collection and modify each item.
Array keys are preserved.
max
Get maximum value from items.
The ">" operator is used to find maximum value.
merge
Merge collection/array to current array.
In associative arrays values for existing keys will be overwrite. In indexed arrays new values are always appended at the end of collection.
min
Get minimum value from items.
The "<" operator is used to find minimum value.
randomKey
Get random key from collection.
Returns null if collection is empty.
random
Get random item value.
Returns null if collection is empty.
reduce
Reduces the collection to a single value, passing the result of each iteration into the subsequent iteration.
removeFirst
Remove first N items from collection.
removeLast
Remove last N items from collection.
reverse
Reverse items order.
skip
Skip N first items.
sortDesc
Sort items descending.
Strings are sorted in case insensitive manner.
See also sort
method.
sort
Sort items ascending.
Strings are sorted in case insensitive manner.
See also sortDesc
method.
sum
Returns the sum of all items in the collection.
Throws when trying to calculate sum from non-number value.
take
Take N first items.
toArray
Returns collection's items.
unique
Left only items with unique value.
First occurrence is taken if multiple same values are encountered.
values
Returns all of the collection's values (as indexed array).
whereIn
Return collection with items that needle is in haystack of accepted values.
where
Return collection with items which match given criteria.
All versions of collections with dependencies
ext-mbstring Version *