PHPackages                             camillebaronnet/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. camillebaronnet/collection

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

camillebaronnet/collection
==========================

v0.4.0(4y ago)03.1k↓23.8%mitPHP

Since Apr 3Pushed 2mo ago1 watchersCompare

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

READMEChangelog (1)Dependencies (2)Versions (6)Used By (0)

Warning

**\[Archived\]** Since the arrival of PHP pipe operators, use a true pipe-native approach with a library such as:

Collection
==========

[](#collection)

Collection provides a powerful data manipulation system based on the map/reduce/filter principles. Thanks to the php generators and the immutability, it allows to calculate the data as late as possible while preserving the memory.

Installation
------------

[](#installation)

```
composer require camillebaronnet/collection
```

How to use
----------

[](#how-to-use)

```
public Collection::__construct(...$elements)
public Collection::__construct(\Traversable|array $elements)
```

Exemple :

```
$collection = (new Collection('foo', 'bar'))
    ->map(fn($word) => 'hello '.$word)
    ->map(fn($word) => strtoupper($word))
;

// The data is computed only here
var_dump($collection->toArray());
```

```
array(2) {
  [0]=>
  string(9) "HELLO FOO"
  [1]=>
  string(9) "HELLO BAR"
}

```

Basic operations
----------------

[](#basic-operations)

- [map(callable): Collection](#map)
- [filter(callable): Collection](#filter)
- [reduce(callable, initial): mixed](#reduce)
- flatten(): Collection
- flatMap(callable): Collection
- sort(callable = null): Collection
- sum(): int|float
- avg(): int|float
- unique(): Collection
- count(): int
- first(): mixed
- each(): Collection
- [groupBy(callable): CollectionGroup](#groupby)

---

**Note :** As long as you use map/filter operations, nothing will be executed or stored in memory. If you decide to do more than one reduce or iterate operation, use `get()`first to before to buffer the previous operations.

---

### Map

[](#map)

The `map()` function iterates over each element and applies the transformation function provided by the user.

```
map(fn($element) => /* ... */);
```

Example :

```
(new Collection(10, 50, 100))
    ->map(fn($element) => $element * 2)
    ->toArray()
;

// [20, 100, 200]
```

### Filter

[](#filter)

The `filter()` method test elements using that pass the test implemented by the provided function.

```
filter(fn ($element) => /* ... */);
```

Example :

```
(new Collection(10, 50, 100))
    ->filter(fn($element) => $element > 20)
    ->toArray()
;
// [50, 100]
```

### Reduce

[](#reduce)

The `reduce()` method executes a reduction function provided by the user on each element, the result of each iteration is passed to the next iteration.

```
reduce(fn($carry, $currentValue, $currentKey) => /* ... */, $initial);
```

Example :

```
(new Collection(10, 50, 100))
    ->reduce(fn($carry, $current) => $carry + $current)
;
// 160
```

### GroupBy

[](#groupby)

The method `groupBy()` returns a Collection of CollectionGroup. A CollectionGroup is a simple collection with a `key` property added.

```
groupBy(fn ($element) => /* ...  */);
```

Example :

```
$result = (new Collection([
        ['key1' => 'foo', 'key2' => 10],
        ['key1' => 'bar', 'key2' => 11],
        ['key1' => 'foo', 'key2' => 12],
        ['key1' => 'bar', 'key2' => 14],
    ]))
    ->groupBy(fn($x) => $x['key1'])
    ->toArray() // [CollectionGroup, CollectionGroup]
;

$result[0]->key; // 'foo'
$result[0]->toArray(); // [['key1' => 'foo', 'key2' => 10],['key1' => 'foo', 'key2' => 12],]
```

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance57

Moderate activity, may be stable

Popularity22

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity42

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

Total

5

Last Release

1494d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/647a717d67e0f2742e4cb778effba9fb33d1f762dc57e39f3955772655363267?d=identicon)[camillebaronnet](/maintainers/camillebaronnet)

---

Top Contributors

[![camillebaronnet](https://avatars.githubusercontent.com/u/1884268?v=4)](https://github.com/camillebaronnet "camillebaronnet (16 commits)")

---

Tags

collectiongeneratorimmutablemapphpreducestream

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[mundschenk-at/php-typography

A PHP library for improving your web typography

78945.6k13](/packages/mundschenk-at-php-typography)

PHPackages © 2026

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