Download the PHP package originphp/collection without Composer
On this page you can find all versions of the php package originphp/collection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download originphp/collection
More information about originphp/collection
Files in originphp/collection
Package collection
Short Description OriginPHP Collection
License MIT
Homepage https://www.originphp.com
Informations about the package collection
Collection
You can create a Collection
using arrays or an object which is an instance of Traversable.
Installation
To install this package
Creating a Collection
To create a collection:
There is also a helper function which you can use.
Collections
After you have finished manipulating the data you can use toArray
or toList
to convert the collection. Some methods return a boolean (e.g. every) or number (e.g median, average etc.), however most will return a new collection, which then can be chained through other methods.
Iteration Methods
Extract
Extracts a single column from a collection to create a list. You can use dot notation.
You can also use a callback function :
Each
Go through each item of the collection. You should note that each does not modify data. If you want to modify data then use map
.
Map
This will iterate through each item in the collection and pass value through a callback which can modify the data and return it, creating a new collection in the process.
Combine
Creates a new collection using keys and values.
Results from combine can also be grouped by a third key.
Chunk
Chunks a collection into multiple parts
Filter Methods
Filter
Filters results using a callback function
Reject
This is the inverse of filter.
Every
Run truth tests on every item in the collection.
Some
Check to see if at least one item matches the filter
Sorting
SortBy
Sorts a collection by a field or callback. To sort by a field, and you can use dot notation.
To sort by a callback.
The sortBy method accepts 3 arguments, with the first argument being the path or a callback.
The second argument is the direction,which can be either SORT_DESC
or SORT_ASC
.
The third argument depends upon the data and the same flags used by PHP Sort, which include:
SORT_NUMERIC
- for numbersSORT_STRING
- for stringsSORT_NATURAL
- for natural ordering
Aggregation
Min
Gets the first item with the smallest value.
To sort by a callback.
Max
Gets the first item with the smallest value.
To sort by a callback.
Counting
SumOf
Gets the sum from a field or callback.
To get the sum using a callback
Avg
Gets the average value from a field or callback.
To get the average value using a callback
Median
Gets the median value from a field or callback.
To get the median value using a callback
Count
This a function to count items in the collection, it is useful when working with other collection methods such as take or chunk.
CountBy
Counts by a field and value, and results are grouped.
You can also use a callback.
Grouping
GroupBy
Groups results by a field or callback.
You can also use a callback.
Inserting Data
Insert
Inserts a value into a path for each item in the collection.
Taking the same example, I will chain it using the helper function. This can be done with any method that returns a new collection.
Other
Take
Take a number of items from a collection.