PHPackages                             collector/collector - 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. collector/collector

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

collector/collector
===================

A collection library inspired by Laravel Collections, built using generators to preserve memory

0.1.1(10y ago)15208MITPHPPHP ^5.6 | ^7.0

Since Jun 3Pushed 10y ago1 watchersCompare

[ Source](https://github.com/AydinHassan/collector)[ Packagist](https://packagist.org/packages/collector/collector)[ RSS](/packages/collector-collector/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

Collector
=========

[](#collector)

A collection library, inspired by Laravel, [Refactoring to Collections](http://adamwathan.me/refactoring-to-collections/), this [tweet](https://twitter.com/fschmengler/status/738254612076101633) and my own need/desire for it. Built using generators to preserve memory when dealing with large data sets.

This was put together in about 2 hours - unit tests ripped straight from Laravel - I'd like to implement as much of the Laravel Collection class as possible in due time.

Create a collection
-------------------

[](#create-a-collection)

Use the static helper method `collect` to create the collection, valid arguments are any objects implementing `\Iterator` including generators, any callable which can resolve to a generator or an array.

```
//iterator
$collection = Collector::collect(new ArrayIterator([1, 2, 3]);

//callable generator
$collection = Collector::collect(function () {
    yield 1;
    yield 2;
    yield 3;
});

//array
$collection = Collector::collect([1, 2, 3]);
```

You can also use the more explicit static constructors if you wish:

```
//iterator
$collection = Collector::fromIterator(new ArrayIterator([1, 2, 3]);

//callable generator
$collection = Collector::fromCallable(function () {
    yield 1;
    yield 2;
    yield 3;
});

//array
$collection = Collector::fromArray([1, 2, 3]);
```

Operations
==========

[](#operations)

Map
---

[](#map)

```
use Collector\Collector;

$source = function () {
    yield 1;
    yield 2;
    yield 3;
};

$collection = Collector::fromCallable($source)
    ->map(function ($item, $key) {
        return $item * 2;
    });

var_dump($collection->asArray());

array(3) {
  [0] =>
  int(2)
  [1] =>
  int(4)
  [2] =>
  int(6)
}
```

Filter
------

[](#filter)

```
use Collector\Collector;

$source = function () {
    yield 1;
    yield 2;
    yield 3;
};

$collection = Collector::fromCallable($source)
    ->filter(function ($item, $key) {
        return $item >=2;
    });

var_dump($collection->asArray());

array(2) {
  [1] =>
  int(2)
  [2] =>
  int(3)
}
```

Flat Map
--------

[](#flat-map)

```
use Collector\Collector;

$source = function () {
    yield ['name' => 'Aloo Gobi', 'ingredients' => ['Cauliflower', 'Potato']];
    yield ['name' => 'Jelfrezi', 'ingredients' => ['Chicken', 'Tomatoes', 'Chili']];
};

$collection = Collector::fromCallable($source)
    ->flatMap(function ($item, $key) {
        return $item['ingredients'];
    });

var_dump($collection->asArray());

array(5) {
  [0] =>
  string(11) "Cauliflower"
  [1] =>
  string(6) "Potato"
  [2] =>
  string(7) "Chicken"
  [3] =>
  string(8) "Tomatoes"
  [4] =>
  string(5) "Chili"
}
```

Each
----

[](#each)

```
use Collector\Collector;

$source = function () {
    yield ['name' => 'Aloo Gobi', 'ingredients' => ['Cauliflower', 'Potato']];
    yield ['name' => 'Jelfrezi', 'ingredients' => ['Chicken', 'Tomatoes', 'Chili']];
};

$collection = Collector::fromCallable($source)
    ->each(function ($item, $key) {
        echo $item['name'] . "\n";
    });

//Outputs:
Aloo Gobi
Jelfrezi
```

Collapse
--------

[](#collapse)

```
use Collector\Collector;

$source = function () {
    yield ['garlic', 'chili', 'ginger', 'coriander'];
    yield ['onions', 'tomatoes'];
};

$collection = Collector::fromCallable($source)
    ->collapse();

var_dump($collection->asArray());

array(6) {
  [0] =>
  string(6) "garlic"
  [1] =>
  string(5) "chili"
  [2] =>
  string(6) "ginger"
  [3] =>
  string(9) "coriander"
  [4] =>
  string(6) "onions"
  [5] =>
  string(8) "tomatoes"
}
```

Flip
----

[](#flip)

```
use Collector\Collector;

$source = function () {
    yield 'Aydin';
    yield 'Caroline';
};

$collection = Collector::fromCallable($source)
    ->flip();

var_dump($collection->asArray());

array(2) {
  'Aydin' =>
  int(0)
  'Caroline' =>
  int(1)
}
```

Key By
------

[](#key-by)

```
use Collector\Collector;

$source = function () {
    yield ['name' => 'Vienna'];
    yield ['name' => 'Lower Austria'];
    yield ['name' => 'Styria'];
};

$collection = Collector::fromCallable($source)
    ->keyBy(function ($item, $key) {
        return $item['name'];
    });

var_dump($collection->asArray());

array(3) {
  'Vienna' =>
  array(1) {
    'name' =>
    string(6) "Vienna"
  }
  'Lower Austria' =>
  array(1) {
    'name' =>
    string(13) "Lower Austria"
  }
  'Styria' =>
  array(1) {
    'name' =>
    string(6) "Styria"
  }
}
```

Values
------

[](#values)

```
use Collector\Collector;

$source = function () {
    yield 5 => 'Vienna';
    yield 1 => 'Lower Austria';
    yield 9 => 'Styria';
};

$collection = Collector::fromCallable($source)
    ->values();

var_dump($collection->asArray());

array(3) {
  [0] =>
  string(6) "Vienna"
  [1] =>
  string(13) "Lower Austria"
  [2] =>
  string(6) "Styria"
}
```

Keys
----

[](#keys)

```
use Collector\Collector;

$source = function () {
    yield 5 => 'Vienna';
    yield 1 => 'Lower Austria';
    yield 9 => 'Styria';
};

$collection = Collector::fromCallable($source)
    ->keys();

var_dump($collection->asArray());

array(3) {
  [0] => int(5)
  [1] => int(1)
  [2] => int(9)
}
```

Zip
---

[](#zip)

```
use Collector\Collector;

$countries = function () {
    yield 'Austria';
    yield 'Kyrgyzstan';
    yield 'Burkina Faso';
};

$continents = function () {
    yield 'Europe';
    yield 'Asia';
    yield 'Africa';
};

$zipped = Collector::fromCallable($countries)
    ->zip(Collector::fromCallable($continents));

foreach ($zipped as $zipPart) {
    var_dump($zipPart->asArray());
}

array(2) {
  [0] =>
  string(7) "Austria"
  [1] =>
  string(6) "Europe"
}

array(2) {
  [0] =>
  string(10) "Kyrgyzstan"
  [1] =>
  string(4) "Asia"
}

array(2) {
  [0] =>
  string(12) "Burkina Faso"
  [1] =>
  string(6) "Africa"
}
```

Unit tests
==========

[](#unit-tests)

```
git clone git@github.com:AydinHassan/collector.git
cd collector
composer install
composer test
```

Benchmarks
==========

[](#benchmarks)

There are some bench marks included showing performance and memory consumption metrics of the collection against native operations.

```
git clone git@github.com:AydinHassan/collector.git
cd collector
composer install
./vendor/bin/phpbench run benchmarks/LoopOneHundredThousandItems.php --report=default
./vendor/bin/phpbench run benchmarks/MapOneMillionItems.php --report=default
./vendor/bin/phpbench run benchmarks/FilterOneMillionItems.php --report=default
```

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 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 ~4 days

Total

2

Last Release

3674d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2817002?v=4)[Aydin Hassan](/maintainers/AydinHassan)[@AydinHassan](https://github.com/AydinHassan)

---

Top Contributors

[![AydinHassan](https://avatars.githubusercontent.com/u/2817002?v=4)](https://github.com/AydinHassan "AydinHassan (10 commits)")

---

Tags

arraygeneratoriteratormapcollectioncollections

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/collector-collector/health.svg)

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

###  Alternatives

[athari/yalinqo

YaLinqo, a LINQ-to-objects library for PHP

4531.2M5](/packages/athari-yalinqo)[aimeos/map

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

4.3k443.7k14](/packages/aimeos-map)[loophp/collection

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

743730.3k15](/packages/loophp-collection)[chdemko/sorted-collections

Sorted Collections for PHP &gt;= 8.4

222.6M3](/packages/chdemko-sorted-collections)[rotexsoft/versatile-collections

A collection package that can be extended to implement things such as a Dependency Injection Container, RecordSet objects for housing database records, a bag of http cookies, or technically any collection of items that can be looped over and whose items can each be accessed using array-access syntax or object property syntax.

186.1k1](/packages/rotexsoft-versatile-collections)[graze/data-structure

Data collections and containers

12293.1k10](/packages/graze-data-structure)

PHPackages © 2026

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