Collection is a library aimed at providing you easy tools to handle your PHP arrays.
Just add chindit/collection to your composer.json
composer require chindit/collection
To initiate a collection, just call the constructor with your array as parameter
$myCollection = new Chindit\Collection(['a', 'b' => 'c']);
all() mixed : return all values of your collectioncontains($search) bool : true if any element of your collection is strictly equal to $search. False otherwisecount() int : number of elements in your collectioncurrent() mixed : element on which iterator is pointing. First element if not accessed beforeeach($callable) Collection : apply $callable on every element of your collection. If $callable returns false, processing is interruptedfilter($callable) Collection : return a copy of your collection with only the elements for which $callable returned truefirst() mixed : first element of the collection (equals to $array[0])flatten($depth = 500) Collection : flatten the colletion until $depth level. $depth must be an int. All sub-collections will be merged to parent levelget($key, $defaultValue = null) mixed : return the value for the $key element. If $key element doesn’t exist, $defaultValue will be returned instead.groupBy($key) Collection : return a copy of your collection, grouped by $keyhas($key) bool : check if $key is a valid key for this collectionisEmpty() bool : wether the collection is emptyisNotEmpty() bool : wether the collection is not emptykey() mixed : key for the current element in iteratorkeyBy($key) Collection : rewrite keys based on $key. $key can be either a callable (in this case, key will be the value returned by the function) or, in the case of an object, a property of this object.keys() Collection : return all keys for the collectionmap($callable) Collection : apply $callable on each element of the collectionmerge($collection) Collection : merge $collection into actual collectionmergeRecursive($collection) Collection : merge recursively $collection into actual collectionnext() void : move iterator to next elementpluck($string) Collection : take only $string value for all elements of the collection and create a collection for these elementspush($element) Collection : add $element to actual collectionput($key, $elemnt) Collection : add $element to actual collection and set its key to $keyrewind() void : rewind iteratorrsort() Collection : sort collection by reverse ordersort() Collection : sort collectiontoArray() array : transfort actual collection to an arrayunique() Collection : return a collection with all unique elements in this collectionvalid() bool : check if iterator is in a valid position// Let's assume we have some Car objects
$myObject = new Car();
$myCollection = new Collection($arrayOfCarObjects);
// «pluck» will access «brand» property or «getBrand» method on all elements of the collection and return its value
// «unique» will remove all duplicates
$uniqueBrands = $myCollection->pluck('brand')->unique();
If you have any issue or question with this repository, do not hesitate to leave a comment in the «Issue» sections ^^