PHPackages                             cardinal-collections/cardinal-collections - 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. cardinal-collections/cardinal-collections

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

cardinal-collections/cardinal-collections
=========================================

Cardinal Collections for PHP

v1.0.1(1y ago)78.2k↓30.8%12MITPHPPHP &gt;=7.1

Since Aug 30Pushed 1y ago2 watchersCompare

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

READMEChangelog (2)Dependencies (1)Versions (9)Used By (2)

Cardinal Collections for PHP
============================

[](#cardinal-collections-for-php)

[![Build Status (GitHub Actions)](https://github.com/vrza/cardinal-collections/actions/workflows/build.yml/badge.svg)](https://github.com/vrza/cardinal-collections/actions)

A toolkit for building custom PHP collections.

Included are:

- two fast and reusable generic iterator implementations in pure PHP
- implementations of mutable Map and Set, but with anything as key
- utilities for working with iterables
- utilities for hashing arrays and objects

Map and Set features
--------------------

[](#map-and-set-features)

Reduce, map and filter:

```
$set = new Set([1, 3, 5, 7]);
$sum = $set->reduce(function ($acc, $x) {
    return $acc + $x;
});
echo $sum;
// output: 16
```

```
$set = new Set([0, 1, 2, 3]);
$squares = $set->map(function ($x) {
    return $x * $x;
});
echo $squares;
// output: { 0, 1, 4, 9 }
```

```
$set = new Set([0, 1, 2, 3, 4, 5, 6, 7]);
$odd = $set->filter(function($x) {
    return $x % 2 == 1;
});
echo $odd;
// output: { 1, 3, 5, 7 }
```

Collections implement `ArrayAccess`, `Iterator` and `Countable` interfaces, so in many ways you can work with them like you work with standard PHP arrays:

```
$map[] = ['foo' => 'bar'];
$value = $map[0];
unset($map[0]);
count($map);
foreach ($map as $key => $value);
```

Mutations while iterating work as expected:

```
$set = new Set([ 1, 2, 3 ]);
$previous = false;
foreach ($set as $elem) {
    if ($previous) $set->remove($previous);
    $previous = $elem;
}
echo $set;
// output: { 3 }
```

map/filter/reduce are implemented as a trait that you can add to your own collections.

Comparing vanilla PHP to Cardinal Collections
---------------------------------------------

[](#comparing-vanilla-php-to-cardinal-collections)

### Example 1

[](#example-1)

We read code that uses vanilla PHP array functions by starting from inner arguments and moving outward:

```
array_values(array_map(function ($cId) { return new ObjectId($cId); }, $cIds));
```

Cardinal Collections object-oriented approach with method chaining results in code that reads from left to right:

```
(new Set($cIds))->map(function ($cId) { return new ObjectId($cId); })->values();
```

### Example 2

[](#example-2)

Vanilla PHP `array_map()` only maps array values:

```
$pidsToExitCodes = reapAnyChildren();
$jobIds = array_map(function ($pid) {
    return handleTerminatedProcess($pid);
}, array_keys($pidsToExitCodes));
```

Cardinal Collections `Map::map()` allows us to map keys and values, so we can easily refactor the above code in order to process additional information (in this example, exit codes) with no added code complexity:

```
$pidsToExitCodes = new Map(reapAnyChildren());
$pidsToJobIds = $pidsToExitCodes->map(function ($pid, $exitCode) {
    return [$pid, handleTerminatedProcess($pid, $exitCode)];
});
```

API Reference
-------------

[](#api-reference)

### Map

[](#map)

#### Constructor

[](#constructor)

```
Map(iterable $iterable = [])
```

Creates a new Map, initialized using keys and values from the given iterable. If called without arguments, an empty Map is created.

#### put

[](#put)

```
put($key, $value): Map
```

Adds an element to the Map. Returns the Map with the added element.

#### add

[](#add)

```
add($key, $value): Map
```

An alias for `put`.

#### putIfAbsent

[](#putifabsent)

```
putIfAbsent($key, $value)
```

If the key is not in the Map, adds a new key/value pair, returning null. If the key is present in the Map, returns the existing value.

#### append

[](#append)

```
append($value): Map
```

Appends a value to the Map, using the next available numeric key. Returns the Map.

#### remove

[](#remove)

```
remove($key): Map
```

Removes a key from the Map, returning the Map.

#### delete

[](#delete)

```
delete($key): Map
```

An alias for `remove`.

#### get

[](#get)

```
get($key, $default = null)
```

Returns the value associated with the key, or `$default` value if the key is not present in the Map.

#### count

[](#count)

```
count(): int
```

Returns the number of elements in the Map.

#### isEmpty

[](#isempty)

```
isEmpty(): bool
```

Returns true if the Map is empty.

#### nonEmpty

[](#nonempty)

```
nonEmpty(): bool
```

Returns true if the Map contains at least one element.

### Set

[](#set)

```
Set(iterable $iterable = [])
```

Creates a new Set, initialized using values of the given iterable. If called without arguments, an empty Set is created.

#### add

[](#add-1)

```
add($element): Set
```

Adds an element to the Set. Returns the Set with added element.

#### has

[](#has)

```
has($element): bool
```

Returns true if the element is a member of the Set.

#### contains

[](#contains)

```
contains($element): bool
```

An alias for `has`.

#### remove

[](#remove-1)

```
remove($element): Set
```

Removes an element from the Set, returning the Set without the element.

#### delete

[](#delete-1)

```
delete($key): Set
```

An alias for `remove`.

#### equals

[](#equals)

```
equals(Set $otherSet): bool
```

Returns true if Set and `$otherSet` are equal.

#### subsetOf

[](#subsetof)

```
subsetOf(Set $otherSet): bool
```

Returns true if Set is a subset of `$otherSet`.

#### union

[](#union)

```
union(Set $otherSet): Set
```

Returns the union of Set and `$otherSet`.

#### intersect

[](#intersect)

```
intersect(Set $otherSet): Set
```

Returns the intersection of Set and `$otherSet`.

#### difference

[](#difference)

```
difference(Set $otherSet): Set
```

Returns a new Set with all members of Set that are not members of `$otherSet`.

#### count

[](#count-1)

```
count(): int
```

Returns the number of elements in the Set.

#### isEmpty

[](#isempty-1)

```
isEmpty(): bool
```

Returns true if the Set is empty.

#### nonEmpty

[](#nonempty-1)

```
nonEmpty(): bool
```

Returns true if the Set contains at least one element.

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

[](#installation)

Assuming you have PHP Composer installed, and that the `composer` executable is in your `$PATH`:

```
composer require cardinal-collections/cardinal-collections
```

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance33

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 99.2% 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 ~157 days

Total

5

Last Release

676d ago

Major Versions

v0.9.2 → v1.0.02023-12-07

### Community

Maintainers

![](https://www.gravatar.com/avatar/b842fcccb4fa8eb90b6d6645e9fe1066b59ca01b3c38aa90c3b80a5ce07d02fe?d=identicon)[vrza](/maintainers/vrza)

---

Top Contributors

[![vrza](https://avatars.githubusercontent.com/u/1699472?v=4)](https://github.com/vrza "vrza (118 commits)")[![Martyrer](https://avatars.githubusercontent.com/u/3949635?v=4)](https://github.com/Martyrer "Martyrer (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[breadlesscode/neos-nodetypes-folder

Folder nodetype for Neos CMS

1088.5k1](/packages/breadlesscode-neos-nodetypes-folder)

PHPackages © 2026

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