PHPackages                             debuss-a/enumerable - 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. debuss-a/enumerable

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

debuss-a/enumerable
===================

A library that provides a fluent and expressive API for working with collections of data.

1.0.0(1y ago)01MITPHPPHP ^8.2CI passing

Since Apr 27Pushed 1y ago1 watchersCompare

[ Source](https://github.com/debuss/enumerable)[ Packagist](https://packagist.org/packages/debuss-a/enumerable)[ Docs](https://github.com/debuss/enumerable)[ RSS](/packages/debuss-a-enumerable/feed)WikiDiscussions master Synced 1mo ago

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

Enumerable Library
==================

[](#enumerable-library)

A library that provides an `Enumerable` interface and a `Collection` class inspired by the `Enumerable` class in .NET.
It offers a fluent and expressive API for working with collections of data.

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

[](#installation)

Install the package via [Composer](https://getcomposer.org/):

```
composer require debuss-a/enumerable
```

Features
--------

[](#features)

- Fluent and expressive API for data manipulation.
- Inspired by .NET's `Enumerable` class.
- Supports PHP 8.2+.
- Includes a variety of methods for filtering, transforming, and combining collections.

Usage
-----

[](#usage)

```
use Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);

// Example: Filter even numbers
$evenNumbers = $collection->where(fn($item) => $item % 2 === 0);

print_r($evenNumbers->toArray()); // [2, 4]
```

Methods
-------

[](#methods)

Below is a list of all available methods with examples:

### Instantiation

[](#instantiation)

Different ways to create a `Collection`:

```
Collection::empty(); // Creates an empty collection
Collection::repeat('hello', 3); // Creates a collection with 3 elements, all being 'hello'
Collection::fromArray([1, 2, 3]); // Creates a collection from an array
Collection::fromRange(1, 5); // Creates a collection from a range
Collection::fromJson('{"a": 1, "b": 2}'); // Creates a collection from a JSON string
```

### `all(): bool`

[](#all-bool)

Checks if all items match a condition.

```
$allEven = $collection->all(fn($item) => $item % 2 === 0);
```

### `equals(Enumerable $second): bool`

[](#equalsenumerable-second-bool)

Checks if two collections are equal.

```
$first = new Collection([1, 2, 3]);
$second = new Collection([1, 2, 3, 4, 5]);
$areEqual = $first->equals($second); // False

$second = new Collection([1, 2, 3]);
$areEqual = $first->equals($second); // True
```

### `equalBy(Enumerable $second, callable $callable): bool`

[](#equalbyenumerable-second-callable-callable-bool)

Checks if two collections are equal based on a condition.

```
$first = Collection::fromArray([1.0, 2.0, 3.0]);
$second = Collection::fromArray([1, 2, 3]);
$areEqual = $first->equalBy($second, fn($x, $y) => (int)$x == (int)$y); // True
```

### `any(callable $callable): bool`

[](#anycallable-callable-bool)

Checks if any item matches a condition.

```
$collection = Collection::fromArray(['airplane', 'car']);
$any = $collection->any(fn($item) => $item == 'car')); // True
```

### `average(): int|float`

[](#average-intfloat)

Calculates the average of numeric items.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$average = $collection->average(); // 3
```

### `max(): int|float`

[](#max-intfloat)

Returns the maximum value in the collection.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$max = $collection->max(); // 5
```

### `min(): int|float`

[](#min-intfloat)

Returns the minimum value in the collection.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$max = $collection->min(); // 1
```

### `sum(): int|float`

[](#sum-intfloat)

Returns the sum of values in the collection.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$max = $collection->sum(); // 15
```

### `count(): int`

[](#count-int)

Returns the number of items in the collection.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$count = $collection->count(); // 5
```

### `order(): Collection`

[](#order-collection)

Sorts the collection in ascending order.

```
$collection = Collection::fromArray([5, 3, 1, 4, 2]);
$sorted = $collection->order(); // [1, 2, 3, 4, 5]
```

### `orderBy(callable $callable): Collection`

[](#orderbycallable-callable-collection)

Sorts the collection based on a key generated by the callable.

```
$collection = Collection::fromArray(['pineapple', 'banana', 'strawberry']);
$sorted = $collection->orderBy(fn($item) => strlen($item)); // ['banana', 'pineapple', 'strawberry']
```

### `orderDescending(): Collection`

[](#orderdescending-collection)

Sorts the collection in descending order.

```
$collection = Collection::fromArray([5, 3, 1, 4, 2]);
$sorted = $collection->orderDescending(); // [5, 4, 3, 2, 1]
```

### `orderDescendingBy(callable $callable): Collection`

[](#orderdescendingbycallable-callable-collection)

Sorts the collection based on a key generated by the callable in descending order.

```
$collection = Collection::fromArray(['pineapple', 'banana', 'strawberry']);
$sorted = $collection->orderDescendingBy(fn($item) => strlen($item)); // ['strawberry', 'pineapple', 'banana']
```

### `reverse(): Collection`

[](#reverse-collection)

Reverses the order of items in the collection.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$reversed = $collection->reverse(); // [5, 4, 3, 2, 1]
```

### `append(mixed $item): Collection`

[](#appendmixed-item-collection)

Adds an item to the end of the collection.

```
$collection = Collection::fromArray([1, 2, 3]);
$collection = $collection->append(4);
print_r($collection->toArray()); // [1, 2, 3, 4]
```

### `prepend(mixed $item): Collection`

[](#prependmixed-item-collection)

Adds an item to the beginning of the collection.

```
$collection = Collection::fromArray([1, 2, 3]);
$collection = $collection->prepend(0);
print_r($collection->toArray()); // [0, 1, 2, 3]
```

### `chunk(int $length): Collection`

[](#chunkint-length-collection)

Splits the collection into chunks of a specified length.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$chunks = $collection->chunk(2);
print_r($chunks->toArray()); // [[1, 2], [3, 4], [5]]
```

### `concat(Enumerable $second): Collection`

[](#concatenumerable-second-collection)

Concatenates two collections.

```
$other = new Collection([6, 7, 8]);
$concatenated = $collection->concat($other);
print_r($concatenated->toArray()); // [1, 2, 3, 4, 5, 6, 7, 8]
```

### `contains(mixed $item): bool`

[](#containsmixed-item-bool)

Checks if the collection contains a specific item.

```
$collection = Collection::fromArray([1, 2, 3]);
$contains = $collection->contains(2); // True
```

### `countBy(callable $callable): int`

[](#countbycallable-callable-int)

Counts the number of items that match a condition.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$count = $collection->countBy(fn($item) => $item > 2); // 3
```

### `distinct(): Collection`

[](#distinct-collection)

Returns a new collection with distinct items.

```
$collection = Collection::fromArray([1, 2, 2, 3, 4, 4]);
$distinct = $collection->distinct();
print_r($distinct->toArray()); // [1, 2, 3, 4]
```

### `distinctBy(callable $callable): Collection`

[](#distinctbycallable-callable-collection)

Returns a new collection with distinct items based on a key generated by the callable.

```
$collection = Collection::fromArray(['apple', 'banana', 'apple', 'orange']);
$distinct = $collection->distinctBy(fn($item) => $item[0]);
print_r($distinct->toArray()); // ['apple', 'banana', 'orange']
```

### `itemAt(int $index): mixed`

[](#itematint-index-mixed)

Returns the item at a specific index.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$item = $collection->itemAt(2); // 3
```

### `itemAtOrDefault(int $index, mixed $default = null): mixed`

[](#itematordefaultint-index-mixed-default--null-mixed)

Returns the item at a specific index or a default value if the index is out of bounds.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$item = $collection->itemAtOrDefault(10, 'not found'); // 'not found'
```

### `except(Enumerable $items): Collection`

[](#exceptenumerable-items-collection)

Returns a new collection excluding the specified items.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$other = new Collection([2, 3]);
$except = $collection->except($other);
print_r($except->toArray()); // [1, 4, 5]
```

### `intersect(Enumerable $items): Collection`

[](#intersectenumerable-items-collection)

Returns the intersection of two collections.

```
$other = new Collection([3, 4, 5, 6]);
$intersection = $collection->intersect($other);
print_r($intersection->toArray()); // [3, 4, 5]
```

### `first(): mixed`

[](#first-mixed)

Returns the first item in the collection.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
echo $collection->first(); // 1
```

### `firstOrDefault(mixed $default = null): mixed`

[](#firstordefaultmixed-default--null-mixed)

Returns the first item or a default value if the collection is empty.

```
$collection = Collection::empty();
echo $collection->firstOrDefault(1); // 1
```

### `last(): mixed`

[](#last-mixed)

Returns the last item in the collection.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
echo $collection->last(); // 5
```

### `lastOrDefault(mixed $default = null): mixed`

[](#lastordefaultmixed-default--null-mixed)

Returns the last item or a default value if the collection is empty.

```
$collection = Collection::empty();
echo $collection->lastOrDefault(5); // 5
```

### `select(callable $callable): Collection`

[](#selectcallable-callable-collection)

Projects each item into a new form.

```
$collection = Collection::fromArray([1, 2, 3]);
$squared = $collection->select(fn($item) => $item * 2);
print_r($squared->toArray()); // [2, 4, 6]
```

### `single(callable $callable): mixed`

[](#singlecallable-callable-mixed)

Returns the single item that matches the condition.

```
$collection = Collection::fromArray([1, 2, 3]);
echo $collection->single(fn($item) => $item === 3); // 3
```

### `singleOrDefault(callable $callable, mixed $default = null): mixed`

[](#singleordefaultcallable-callable-mixed-default--null-mixed)

Returns the single item that matches the condition or a default value.

```
$collection = Collection::fromArray([1, 2, 3]);
echo $collection->singleOrDefault(fn($item) => $item === 6, 'not found'); // not found
```

### `skip(int $count): Collection`

[](#skipint-count-collection)

Skips the first `$count` items.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$skipped = $collection->skip(2);
print_r($skipped->toArray()); // [3, 4, 5]
```

### `skipLast(int $count): Collection`

[](#skiplastint-count-collection)

Skips the last `$count` items.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$skippedLast = $collection->skipLast(2);
print_r($skippedLast->toArray()); // [1, 2, 3]
```

### `skipWhile(callable $callable): Collection`

[](#skipwhilecallable-callable-collection)

Skips items while the condition is true.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$skippedWhile = $collection->skipWhile(fn($item) => $item < 3);
print_r($skippedWhile->toArray()); // [3, 4, 5]
```

### `take(int $count): Collection`

[](#takeint-count-collection)

Takes the first `$count` items.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$taken = $collection->take(3);
print_r($taken->toArray()); // [1, 2, 3]
```

### `takeLast(int $count): Collection`

[](#takelastint-count-collection)

Takes the last `$count` items.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$takenLast = $collection->takeLast(2);
print_r($takenLast->toArray()); // [4, 5]
```

### `takeWhile(callable $callable): Collection`

[](#takewhilecallable-callable-collection)

Takes items while the condition is true.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$takenWhile = $collection->takeWhile(fn($item) => $item < 4);
print_r($takenWhile->toArray()); // [1, 2, 3]
```

### `union(Enumerable $second): Collection`

[](#unionenumerable-second-collection)

Combines two collections and removes duplicates.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$other = new Collection([4, 5, 6]);
$union = $collection->union($other);
print_r($union->toArray()); // [1, 2, 3, 4, 5, 6]
```

### `where(callable $callable): Collection`

[](#wherecallable-callable-collection)

Filters items based on a condition.

```
$collection = Collection::fromArray([1, 2, 3, 4, 5]);
$filtered = $collection->where(fn($item) => $item > 3);
print_r($filtered->toArray()); // [4, 5]
```

### `zip(Enumerable $second, callable $callable): Collection`

[](#zipenumerable-second-callable-callable-collection)

Combines two collections into one using a callable.

```
$collection = Collection::fromArray([1, 2, 3]);
$other = new Collection(['a', 'b', 'c']);
$zipped = $collection->zip($other, fn($a, $b) => $a.$b);
print_r($zipped->toArray()); // ['1a', '2b', '3c']
```

### `toArray(): array`

[](#toarray-array)

Converts the collection to an array.

```
print_r($collection->toArray()); // [1, 2, 3, 4, 5]
```

License
-------

[](#license)

This project is licensed under the MIT License. See the `LICENSE.md` file for details.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance48

Moderate activity, may be stable

Popularity1

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

Unknown

Total

1

Last Release

387d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0e1a70117520fe10a630d61c750bbe6888d174e9903825e741470f818ee2c5d1?d=identicon)[debuss-a](/maintainers/debuss-a)

---

Top Contributors

[![debuss](https://avatars.githubusercontent.com/u/2537607?v=4)](https://github.com/debuss "debuss (2 commits)")

---

Tags

collectionenumerablelist

###  Code Quality

TestsPest

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/debuss-a-enumerable/health.svg)

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

###  Alternatives

[phpcollection/phpcollection

General-Purpose Collection Library for PHP

1.0k64.0M34](/packages/phpcollection-phpcollection)[spatie/enum

PHP Enums

84529.1M68](/packages/spatie-enum)[graze/sort

A collection of array sorting transforms and functions

12289.6k2](/packages/graze-sort)[bnomei/kirby3-recently-modified

Kirby Section to display recently modified content pages

309.3k](/packages/bnomei-kirby3-recently-modified)[abdielcs/expanded-collection-bundle

Symfony 2 and 3 bundle for rendering a collection of entities as an expanded selectable list

1210.1k](/packages/abdielcs-expanded-collection-bundle)

PHPackages © 2026

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