Collection

Collection

Collection is a small PHP library that provides convenient tools for handling arrays.

The current release requires PHP 8.3 or later.

Installation

Just add chindit/collection to your composer.json

composer require chindit/collection

Usage

To create a collection, pass an array to the constructor:

use Chindit\Collection\Collection;

$collection = new Collection(['a', 'b' => 'c']);

Methods

Examples

// 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();

Collections can be iterated with foreach. A new iterator is created for each loop:

foreach ($myCollection as $key => $value) {
    // use $key and $value
}

Support & Contact

If you have any issue or question with this repository, do not hesitate to leave a comment in the «Issue» sections ^^