PHPackages                             raphhh/trex-collection - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. raphhh/trex-collection

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

raphhh/trex-collection
======================

Helpers to manipulate collections

1.1.0(9y ago)038MITPHPPHP &gt;=5.4

Since Jan 7Pushed 9y ago1 watchersCompare

[ Source](https://github.com/Raphhh/trex-collection)[ Packagist](https://packagist.org/packages/raphhh/trex-collection)[ RSS](/packages/raphhh-trex-collection/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (5)Used By (0)

TRex Collection
===============

[](#trex-collection)

[![Latest Stable Version](https://camo.githubusercontent.com/419195496c7bbee582c428b868c6e760bc5a6b2eea36a598667e92f3c281a2b9/68747470733a2f2f706f7365722e707567782e6f72672f7261706868682f747265782d636f6c6c656374696f6e2f762f737461626c652e737667)](https://packagist.org/packages/raphhh/trex-collection)[![Build Status](https://camo.githubusercontent.com/2dfb14626fa9b28fcdbd53acf8dd75927eb32f597de3f22acaca6556ba6df5d3/68747470733a2f2f7472617669732d63692e6f72672f5261706868682f747265782d636f6c6c656374696f6e2e706e67)](https://travis-ci.org/Raphhh/trex-collection)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/18896205cb910944bf9d604556dfe50f8fde3de44b6c6914dba1a29b1479b928/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f5261706868682f747265782d636f6c6c656374696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Raphhh/trex-collection/)[![Code Coverage](https://camo.githubusercontent.com/77adacfc0ce5a9a26a4a9befcbbf6c1a7bc46d0873fa1d51fd8a0a6d09eb2c86/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f5261706868682f747265782d636f6c6c656374696f6e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Raphhh/trex-collection/)[![Total Downloads](https://camo.githubusercontent.com/a922d88a8662330dd55a22efeb47c3b3d789f74afeb2e046fd1f1ea7dee61a25/68747470733a2f2f706f7365722e707567782e6f72672f7261706868682f747265782d636f6c6c656374696f6e2f646f776e6c6f6164732e737667)](https://packagist.org/packages/raphhh/trex-collection)[![Reference Status](https://camo.githubusercontent.com/1ba068eee8061771f1c3386df3afd86e5e37fe9fd2ba7888db4de1656edb73ee/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f7068702f7261706868683a747265782d636f6c6c656374696f6e2f7265666572656e63655f62616467652e7376673f7374796c653d666c6174)](https://www.versioneye.com/php/raphhh:trex-collection/references)[![License](https://camo.githubusercontent.com/e5d585d51350c90ab118224df42e783ab978526659130df58ea458c965cf7ea3/68747470733a2f2f706f7365722e707567782e6f72672f7261706868682f747265782d636f6c6c656374696f6e2f6c6963656e73652e737667)](https://packagist.org/packages/raphhh/trex-collection)

Install
-------

[](#install)

`$ composer require raphhh/trex-collection`

Usages
------

[](#usages)

### Collection

[](#collection)

`Collection` is just an `ArrayObject` implementing some additional methods.

Because `Collection` is just a facade, you can keep coding with `ArrayObject` (or any object castable into an array) and implement some TRex traits by yourself:

```
use TRex\Collection;

class MyCollection extends \ArrayObject
{
    use CollectionValueAccessorTrait;
    use CollectionKeyAccessorTrait;
    use CollectionFilterTrait;
    use CollectionComparatorTrait;
    use CollectionSorterTrait;
    ...
}
```

#### Filter

[](#filter)

Provide methods to filter a collection.

##### Filter values with a callback: filter

[](#filter-values-with-a-callback-filter)

```
use TRex\Collection\Collection;

$collection = new Collection([
    't-rex',
    'dog',
    'cat',
]);

$result = $collection->filter(function($value){
    return $value === 't-rex';
});

(array)$result; //['t-rex']
```

##### Apply a callback to the values: each

[](#apply-a-callback-to-the-values-each)

```
use TRex\Collection\Collection;

$collection = new Collection([
    't-rex',
    'dog',
    'cat',
]);

$result = $collection->each(function($value){
    return strtoupper($value);
});

(array)$result; //['T-REX', 'DOG', 'CAT']
```

##### Extract a part of the list: extract

[](#extract-a-part-of-the-list-extract)

```
use TRex\Collection\Collection;

$collection = new Collection([
    't-rex',
    'dog',
    'cat',
]);

$result = $collection->extract(1);

(array)$result; //[1 => 'dog', 2 => 'cat']
```

#### Comparator

[](#comparator)

##### Merge any collections: merge, mergeA

[](#merge-any-collections-merge-mergea)

```
$coll1 = new Collection(['t-rex']);
$coll2 = new Collection(['dog']);
$coll3 = new Collection(['cat']);

$result = $coll1->merge($coll2, $coll3);

(array)$result; //['t-rex', 'dog', 'cat']
```

##### Find the difference between any collections: diff, diffA, diffK

[](#find-the-difference-between-any-collections-diff-diffa-diffk)

```
$coll1 = new Collection(['t-rex', 'dog', 'cat']);
$coll2 = new Collection(['dog']);
$coll3 = new Collection(['cat']);

$result = $coll1->diff($coll2, $coll3);

(array)$result; //['t-rex']
```

##### Find the intersection between any collections: intersect, intersectA, intersectK

[](#find-the-intersection-between-any-collections-intersect-intersecta-intersectk)

```
$coll1 = new Collection(['t-rex', 'dog', 'cat']);
$coll2 = new Collection(['t-rex', 'dog']);
$coll3 = new Collection(['t-rex', 'cat']);

$result = $coll1->intersect($coll2, $coll3);

(array)$result; //['t-rex']
```

#### Sorter

[](#sorter)

##### Reindex collection: reindex

[](#reindex-collection-reindex)

```
$collection = new Collection(['a' => 't-rex', 'b' => 'dog', 'c' => 'cat']);
$result = $collection->reindex(); //[0 => ''t-rex', 1 => 'dog', 2 => 'cat']
```

##### Sort collection: sort

[](#sort-collection-sort)

```
$collection = new Collection(['t-rex', 'dog', 'cat']);
$result = $collection->sort($callback); //indexes nex collection
```

##### Group by specific values: groupBy

[](#group-by-specific-values-groupby)

```
class Bar
{
    public $foo;

    public function __construct($foo)
    {
        $this->foo = $foo;
    }
}

$collection = new Collection([
    new Bar('t-rex'),
    new Bar('dog'),
    new Bar('cat'),
]);

//we will split the collection in 2: dinosaurs vs pets
$results = $collection->groupBy(function(Bar $bar){
    if($bar->foo === 't-rex'){
        return 'dinosaur';
    }
    return 'pet';
});

count($result['dinosaur']); // a Collection with 1 Bar with 't-rex'
count($result['pet']); // a Collection with 2 Bar with 'dog' and 'cat'
```

#### Values

[](#values)

##### Retrieve first value: first

[](#retrieve-first-value-first)

```
$collection = new Collection(['t-rex', 'dog', 'cat']);
$collection->first(); //'t-rex'
```

##### Retrieve last value: last

[](#retrieve-last-value-last)

```
$collection = new Collection(['t-rex', 'dog', 'cat']);
$collection->last(); //'cat'
```

##### Check is has value: has

[](#check-is-has-value-has)

```
$collection = new Collection(['t-rex', 'dog', 'cat']);
$collection->has('t-rex'); //true
```

#### Keys

[](#keys)

##### Search keys: search

[](#search-keys-search)

```
$collection = new Collection(['t-rex', 'dog', 'cat']);
$collection->search('t-rex'); //[0]
```

##### Extract keys: keys

[](#extract-keys-keys)

```
$collection = new Collection(['t-rex', 'dog', 'cat']);
$collection->keys(); //[0, 1, 2]
```

### Sorter

[](#sorter-1)

Sort a collection by a property or a method result.

You need to extends the class `TRex\Collection\AbstractSorter`. In the method `invoke`, you have to return the value to sort for each object the collection content.

Note that the TRex sorter implements duck typing. You should create a sorter by key to access to the data, and not by type of collection content. For example, if you want to sort a collection of objects by them property '$foo', you should call your sorter 'FooSorter', anyway the objects the collection content.

```
use TRex\Collection\AbstractSorter;

class Bar
{
    public $foo;

    public function __construct($foo)
    {
        $this->foo = $foo;
    }
}

// we name the sorter with the name of the property $foo, and not the name of the class Bar
class FooSorter extends AbstractSorter
{
    /**
     * calls the property/method/key to extract the value to sort.
     *
     * @param mixed $object
     * @return mixed
     */
    protected function invoke($object)
    {
        //here, we want to sort the collection by the property $foo
        return $object->foo;
    }
}

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

uasort($collection, new FooSorter()); //will sort 'a', 'b', 'c'
```

### Filter

[](#filter-1)

You need to extends the class `TRex\Collection\AbstractFilter`. In the method `invoke`, you have to return the value to compare for each object the collection content.

Note that the TRex filter implements duck typing. You should create a filter by key to access to the data, and not by type of collection content. For example, if you want to sort a collection of objects by them property '$foo', you should call your sorter 'FooSorter', anyway the objects the collection content.

```
use TRex\Collection\AbstractFilter;

class Bar
{
    public $foo;

    public function __construct($foo)
    {
        $this->foo = $foo;
    }
}

// we name the filter with the name of the property $foo, and not the name of the class Bar
class FooFilter extends AbstractFilter
{
    /**
     * @param mixed $object
     * @return mixed
     */
    protected function invoke($object)
    {
        return $object->foo;
    }
}

$collection = [
    new Bar('a'),
    new Bar('b'),
    new Bar('b'),
];

array_filter($collection, new FooFilter('b')); //wil keep only the 'b' ones
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~51 days

Total

4

Last Release

3622d ago

Major Versions

0.2.0 → 1.0.02016-02-16

### Community

Maintainers

![](https://www.gravatar.com/avatar/5fd55d984cf31c25f875f5636331a20f8be77e9443f2fd17fbccf1f9c4fe7a4c?d=identicon)[raphhh](/maintainers/raphhh)

---

Top Contributors

[![Raphhh](https://avatars.githubusercontent.com/u/5206490?v=4)](https://github.com/Raphhh "Raphhh (30 commits)")

---

Tags

collectionarrayobject

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/raphhh-trex-collection/health.svg)

```
[![Health](https://phpackages.com/badges/raphhh-trex-collection/health.svg)](https://phpackages.com/packages/raphhh-trex-collection)
```

###  Alternatives

[phpcollection/phpcollection

General-Purpose Collection Library for PHP

1.0k64.0M34](/packages/phpcollection-phpcollection)[aimeos/map

Easy and elegant handling of PHP arrays as array-like collection objects similar to jQuery and Laravel Collections

4.2k412.9k11](/packages/aimeos-map)[league/period

Time range API for PHP

7335.4M21](/packages/league-period)[loophp/collection

A (memory) friendly, easy, lazy and modular collection class.

745663.8k13](/packages/loophp-collection)[athari/yalinqo

YaLinqo, a LINQ-to-objects library for PHP

4561.2M5](/packages/athari-yalinqo)[lorisleiva/lody

Load files and classes as lazy collections in Laravel.

956.6M9](/packages/lorisleiva-lody)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
