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

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

imunhatep/collection
====================

General-Purpose Collection Library for PHP

2.0.1(3y ago)563.2k—9.4%2GPL-3.0-or-laterPHPPHP &gt;=8.0.0

Since Dec 19Pushed 3y ago1 watchersCompare

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

READMEChangelog (2)Dependencies (3)Versions (27)Used By (0)

PHP Collection
==============

[](#php-collection)

[![SensioLabsInsight](https://camo.githubusercontent.com/5af8b88e752f407809ec2ca6f0d94d1d73246fad7a91b2d68f3a2da06117442a/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f30343438646466642d363866382d346337372d613337622d3266333838333635326136382f6d696e692e706e67)](https://insight.sensiolabs.com/projects/0448ddfd-68f8-4c77-a37b-2f3883652a68)[![Build Status](https://camo.githubusercontent.com/f7acd429d2aad641f775e5171588c02c8567ab6644be47410d4f2f1ba5be82ae/68747470733a2f2f7472617669732d63692e6f72672f496d756e68617465702f7068702d636f6c6c656374696f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Imunhatep/php-collection)

PHP Collection library.

Based on J. M. Schmitt work, refactored and updated - now requires PHP 8.0. Updated with several useful methods like ::flatMap(), ::map(), ::foldLeft(), ::exists(), ::headOption(), ::lastOption(), ::tail() and e.t.c. Inspired by Scala collections. Added types.

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

[](#installation)

PHP Collection can easily be installed via composer (in process)

```
composer require imunhatep/collection
```

or add it to your `composer.json` file.

Note
----

[](#note)

Collections can be seen as more specialized arrays for which certain contracts are guaranteed.

Supported Collections:

- [Sequence](#sequence-anchor)

    - Keys: numerical, consequentially increasing, no gaps
    - Values: anything, duplicates allowed
    - Classes: `Sequence`, `SortedSequence`
- [Map](#map-anchor)

    - Keys: strings or objects, duplicate keys not allowed
    - Values: anything, duplicates allowed
    - Classes: `Map`, `LRUMap`
- [Set](#set-anchor)

    - Keys: not meaningful
    - Values: objects, or scalars, each value is guaranteed to be unique (see Set usage below for details)
    - Classes: `Set`

General Characteristics:

- Collections are mutable (new elements may be added, existing elements may be modified or removed). Specialized immutable versions may be added in the future though.
- Equality comparison between elements are always performed using the shallow comparison operator (===).
- Sorting algorithms are unstable, that means the order for equal elements is undefined (the default, and only PHP behavior).

Usage
-----

[](#usage)

Collection classes provide a rich API.

Sequence
----------------------------------------------------

[](#sequence-)

```
    // Read Operations
    $seq = new Sequence([0, 2, 3, 2]);
    $seq->get(2); // int(3)
    $seq->all(); // [0, 2, 3, 2]

    $seq->head(); // Some(0)
    $seq->tail(); // Some(2)

    // Write Operations
    $seq = new Sequence([1, 5]);
    $seq->get(0); // int(1)

    $seq
      ->update(0, 4);
      ->get(0); // int(4)

    $seq
      ->remove(0)
      ->get(0); // int(5)

    $seq = new Sequence([1, 4]);
    $seq
      ->add(2)
      ->all(); // [1, 4, 2]

    $seq
      ->addAll([4, 5, 2])
      ->all(); // [1, 4, 2, 4, 5, 2]

    // Sort
    $seq = new Sequence([0, 5, 4, 2]);
    $seq
      ->sortWith(fn($a, $b) => ($a - $b));
      ->all(); // [0, 2, 4, 5]
```

Maps
-------------------------------------------

[](#maps-)

```
    // Read Operations
    $map = new Map(['foo' => 'bar', 'baz' => 'boo']);
    $map->get('foo'); // Some('bar')
    $map->get('foo')->get(); // string('bar')
    $map->keys(); // ['foo', 'baz']
    $map->values(); // ['bar', 'boo']

    $map->headOption(); // Some(['key', 'value'])
    $map->head();       // null | ['key', 'value']
    $map->lastOption(); // Some(['key', 'value'])
    $map->last();       // null | ['key', 'value']

    $map->headOption()->getOrElse([]); // ['foo', 'bar']
    $map->lastOption()->getOrElse([]); // ['baz', 'boo']

    $map->tail();            // ['baz' => 'boo']

    iterator_to_array($map); // ['foo' => 'bar', 'baz' => 'boo']
    $map->all()              // ['foo' => 'bar', 'baz' => 'boo']

    // Write Operations
    $map = new Map();
    $map->set('foo', 'bar');
    $map->setAll(['bar' => 'baz', 'baz' => 'boo']);
    $map->remove('foo');

    // Sort
    $map->sortWith('strcmp');

    // Transformation
    $map->map(fn($k,$v) => ($value * 2));

    $map->flatMap(fn($k,$v) => (new Map())->set($k, $v * 2) );

    $map->foldLeft(SomeObject $s, fn($s, $k, $v) => $s->add([$k, $v * 2]))
```

Set
------------------------------------------

[](#set-)

In a Set each value is guaranteed to be unique. The `Set` class supports objects, and scalars as value. Equality is determined via the following steps.

**Equality of Scalars**

```
Scalar are considered equal if ``$a === $b`` is true.

```

```
    class DateTimeHashable extends \DateTime implements HashableInterface
    {
        public function hash(): string
        {
            return $this->format("YmdP");
        }
    }

    $set = new Set();
    $set->add(new DateTimeHashable('today'));
    $set->add(new DateTimeHashable('today'));

    var_dump(count($set)); // int(1) -> the same date is not added twice

    foreach ($set as $date) {
        var_dump($date);
    }

    $set->all();
    $set->addSet($otherSet);
    $set->addAll($someElements);

    // Traverse
    $set->map(function($x) { return $x*2; });
    $set->flatMap(function($x) { return [$x*2, $x*4]; });
```

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 65.1% 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 ~160 days

Recently: every ~533 days

Total

24

Last Release

1211d ago

Major Versions

0.5.7 → 1.0.02016-02-29

1.4.0 → 2.0.02022-11-21

PHP version history (4 changes)0.5.2PHP &gt;=5.4

1.0.0PHP &gt;=5.5.3

1.2.2PHP &gt;=5.6.0

2.0.0PHP &gt;=8.0.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/699c96d324c69946967303fd47f2fb3fc2a11589efa7031298539924e57159a8?d=identicon)[Imunhatep](/maintainers/Imunhatep)

---

Top Contributors

[![schmittjoh](https://avatars.githubusercontent.com/u/197017?v=4)](https://github.com/schmittjoh "schmittjoh (28 commits)")[![stof](https://avatars.githubusercontent.com/u/439401?v=4)](https://github.com/stof "stof (4 commits)")[![CMCDragonkai](https://avatars.githubusercontent.com/u/640797?v=4)](https://github.com/CMCDragonkai "CMCDragonkai (3 commits)")[![asm89](https://avatars.githubusercontent.com/u/657357?v=4)](https://github.com/asm89 "asm89 (3 commits)")[![imunhatep](https://avatars.githubusercontent.com/u/432393?v=4)](https://github.com/imunhatep "imunhatep (2 commits)")[![DCoderLT](https://avatars.githubusercontent.com/u/616735?v=4)](https://github.com/DCoderLT "DCoderLT (1 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")[![staabm](https://avatars.githubusercontent.com/u/120441?v=4)](https://github.com/staabm "staabm (1 commits)")

---

Tags

collectionfunctional-programminglrumapmapphpsequencesetsortedsequencemapsetcollectionlistsequence

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[phpcollection/phpcollection

General-Purpose Collection Library for PHP

1.0k64.0M34](/packages/phpcollection-phpcollection)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49644.8M97](/packages/marc-mabe-php-enum)[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)[phootwork/collection

The phootwork library fills gaps in the php language and provides better solutions than the existing ones php offers.

3924.8M15](/packages/phootwork-collection)[chdemko/sorted-collections

Sorted Collections for PHP &gt;= 8.2

222.5M3](/packages/chdemko-sorted-collections)

PHPackages © 2026

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