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

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

remotelyliving/php-collection
=============================

An immutable collection object for object oriented set operations

1.2.0(5y ago)04[2 PRs](https://github.com/remotelyliving/php-collection/pulls)MITPHPPHP &gt;=7.4

Since May 30Pushed 3y ago1 watchersCompare

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

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

[![Build Status](https://camo.githubusercontent.com/c640abfd464f597773f8968097ef8ae2c134e5956d404face583615bc7bcd09e/68747470733a2f2f7472617669732d63692e636f6d2f72656d6f74656c796c6976696e672f7068702d636f6c6c656374696f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/remotelyliving/php-collection)[![Total Downloads](https://camo.githubusercontent.com/fc40f3e7073653454d53455cd893acb1a6d19bb4eef1dc5fc9e87d11cf7042f8/68747470733a2f2f706f7365722e707567782e6f72672f72656d6f74656c796c6976696e672f7068702d636f6c6c656374696f6e2f646f776e6c6f616473)](https://packagist.org/packages/remotelyliving/php-collection)[![Coverage Status](https://camo.githubusercontent.com/86792dc7d26ab675770175c242392c404eeaed7ae4db0a99b076393297891644/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f72656d6f74656c796c6976696e672f7068702d636f6c6c656374696f6e2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/remotelyliving/php-collection?branch=master)[![License](https://camo.githubusercontent.com/e233ed97f8cf33a40425dd5190876e53330ccf671565313bf0f2dc0581a1cfbf/68747470733a2f2f706f7365722e707567782e6f72672f72656d6f74656c796c6976696e672f7068702d636f6c6c656374696f6e2f6c6963656e7365)](https://packagist.org/packages/remotelyliving/php-collection)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/f765dcbea12c81472b70c0fab0be6f5c8808ef3cd317f0cee63b8e6073730edf/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f72656d6f74656c796c6976696e672f7068702d636f6c6c656374696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/remotelyliving/php-collection/?branch=master)

php-collection
==============

[](#php-collection)

### A lightweight, immutable collection object for set operations

[](#a-lightweight-immutable-collection-object-for-set-operations)

### Use Cases

[](#use-cases)

So there is already a really nice collection library out there. I wanted to write my own for two reasons.

1. I needed less than half of what it offered.
2. It's fun! 2.1 Libraries that load themselves globally as level functions are evil

But also,

If you're tired of using arrays everywhere, this might be for you. If you're jealous of JavaScript's really gorgeous array and object API, this might be for you If the mutability and utter chaos of passing an array around scares you, this might be for you

### Installation

[](#installation)

```
composer require remotelyliving/php-collection
```

### Usage

[](#usage)

#### Collect::collect

[](#collectcollect)

Collections can be sets of numbers, strings, or objects. No arrays allowed. We don't want to get into multi dimensional madness.

```
use RemotelyLiving\PHPCollection\Collection;

$collectionOfStrings = Collection::collect(['foo', 'bar', 'baz']);
$collectionOfNumbers = Collection::collect([1, 2, 3]);
$collectionOfUsers = Collection::collect([$id1 => $user1, $id2 => $user2, $id4 => $user3]);

// we can iterate over collections
foreach ($collectionOfNumbers as $number) {
    echo (string) $number;
}
// outputs 1, 2, 3

// we can also perform set operations
$collectionOfUsers->contains($user1); // true
$collectionOfUsers->has($id1); // true
$collectionOfUsers->get($id1, new User()); // defaults to a new User if not found
$collectionOfUsers->some(fn(User $user) => $user->isVerified()); // true if some users are verified

$collectionOfUsers->filter(fn(User $user, $index) => $index > 1)
    ->map(fn(User $user) => $user->getName())
    ->first();

$collectionOfNumbers->reverse()
    ->all();

$collectionOfNumbers->each(fn(int $number, int $index) => $number * $index)
    ->unique()
    ->all();
```

#### Collection::later

[](#collectionlater)

We can create collections that don't don't fill up until we want something from them.

```
$generator = fn() => yield 1;
$deferredCollect = Collection::later($generator());
```

#### Collection::fill

[](#collectionfill)

We can create a prefilled collection

```
$filled = Collection::fill(0, 100, 'hi!');
```

#### Collection::fromString

[](#collectionfromstring)

We can create a collection from a delineated string

```
$fromString = Collection::fromString('1|2|3|4|5', '|');
```

#### Collection Set Methods

[](#collection-set-methods)

Now to the good stuff! All evaluation or operating methods that take a callable always pass in the value first, then the key. Below is the whole object API. It provides a lot hopefully without overstepping.

You can loop over it just like an array safely. It's `\Traversable` and `iterable`You can also safely `serialize()` and `json_encode()` it.

```
interface CollectionInterface extends \Traversable, \Countable, \IteratorAggregate, \Serializable, \JsonSerializable
{
    public function count(): int;

    public function map(callable $fn): self;

    public function filter(callable $fn): self;

    public function each(callable $fn): self;

    public function reverse(): self;

    /**
     * @return mixed|null
     */
    public function first();

    /**
     * @return mixed|null
     */
    public function last();

    /**
     * @param callable $fn
     * @param null     $initial
     *
     * @return mixed
     */
    public function reduce(callable $fn, $initial = null);

    public function unique(): self;

    public function diff(Collection $collection): self;

    public function merge(Collection $collection): self;

    public function union(Collection $collection): self;

    public function intersect(Collection $collection): self;

    public function sort(callable $comparator = null): self;

    public function kSort(callable $comparator = null): self;

    public function empty(): bool;

    public function all(): array;

    public function deferred(): \Generator;

    public function values(): array;

    public function equals(Collection $collection): bool;

    /**
     * @param string|int $offset
     *
     * @return bool
     */
    public function has($offset): bool;

    /**
     * @param mixed $value
     *
     * @return bool
     */
    public function contains($value): bool;

    public function some(callable $evaluation): bool;

    /**
     * @param string|int $offset
     * @param mixed|null $default
     *
     * @return mixed|null
     */
    public function get($offset, $default = null);

    /**
     * @return mixed
     */
    public function rand();

    /**
     * @param string|int ...$offset
     */
    public function remove(...$offset): self;
}
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

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

Total

3

Last Release

2176d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/15091ba12470e0054da94a253c264751ef495c9897c59ea6837e986b31606e63?d=identicon)[remotelyliving](/maintainers/remotelyliving)

---

Tags

phparraymapcollection

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[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)[graze/data-structure

Data collections and containers

12287.4k8](/packages/graze-data-structure)[chdemko/sorted-collections

Sorted Collections for PHP &gt;= 8.2

222.5M3](/packages/chdemko-sorted-collections)[iteks/laravel-enum

A comprehensive Laravel package providing enhanced enum functionalities, including attribute handling, select array conversions, and fluent facade interactions for robust enum management in Laravel applications.

2516.7k](/packages/iteks-laravel-enum)

PHPackages © 2026

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