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

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

jdecool/collection
==================

Just an another collections library

127PHP

Since Dec 14Pushed 5y ago1 watchersCompare

[ Source](https://github.com/jdecool/collection)[ Packagist](https://packagist.org/packages/jdecool/collection)[ RSS](/packages/jdecool-collection/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Collection
==========

[](#collection)

[![Build Status](https://github.com/jdecool/collection/workflows/CI/badge.svg)](https://github.com/jdecool/collection/actions?query=workflow%3ACI)[![Latest Stable Version](https://camo.githubusercontent.com/59de54127535034083936f04a9b6e74cd379f5707cc1107f1d335fffb920fcfd/68747470733a2f2f706f7365722e707567782e6f72672f6a6465636f6f6c2f636f6c6c656374696f6e2f762f737461626c652e706e67)](https://packagist.org/packages/jdecool/collection)

Provide a fluent collection library.

Available methods
-----------------

[](#available-methods)

### all

[](#all)

Get all items of the collection

```
$collection = new Collection([0, 1, 2, 3]);
$collection->all(); // [0, 1, 2, 3]
```

### contains

[](#contains)

Determine if an item exists in the collection

```
$collection = new Collection([0, 1, 2, 3]);
$collection->contains(1); // true
$collection->contains(5); // false
```

### count

[](#count)

Count the number of items in the collection

```
$collection = new Collection();
$collection->count(); // 0

$collection = new Collection([0, 1, 2, 3]);
$collection->count(); // 4
```

### diff

[](#diff)

Computes the difference of items in the collection

```
$collection = new Collection(['foo' => 'bar', 'john' => 'doe', 'jane' => 'doe']);
$collection->diff('foo'); // Collection(['foo' => 'bar', 'john' => 'doe', 'jane' => 'doe'])

$collection = new Collection(['foo' => 'bar', 'john' => 'doe', 'jane' => 'doe']);
$collection->diff('doe'); // Collection(['foo' => 'bar'])
```

### diffKeys

[](#diffkeys)

Computes the difference of keys in the collection

```
$collection = new Collection(['foo' => 'bar', 'john' => 'doe', 'jane' => 'doe']);
$collection->diffKeys('foo'); // Collection(['foo' => 'bar', 'john' => 'doe', 'jane' => 'doe'])

$collection = new Collection(['foo' => 'bar', 'john' => 'doe', 'jane' => 'doe']);
$collection->diffKeys('foo' => 'bar'); // Collection(['john' => 'doe', 'jane' => 'doe'])
```

### filter

[](#filter)

Filter the collection

```
$collection = new Collection(['foo' => 'bar', 'john' => 'doe', 'test' => 'bar']);
$collection->filter(function($item) {
    return $item === 'bar';
}); // Collection(['foo' => 'bar', 'test' => 'bar'])
```

### first

[](#first)

Search first element

```
$collection = new Collection([
    ['a' => '1', 'foo' => 'a'],
    ['a' => '2', 'foo' => 'b'],
    ['a' => '3', 'foo' => 'c'],
    ['a' => '4', 'foo' => 'b'],
]);

$collection->first(); // ['a' => '1', 'foo' => 'a']

$collection->first(function($item) {
    return $item['foo'] === 'b';
}); // ['a' => '2', 'foo' => 'b']

$collection->first(/* ... */, 'default value if not found')
```

### flip

[](#flip)

Exchanges all keys with their associated values in an array

```
$collection = new Collection(['foo' => 'bar', 'john' => 'doe']);
$collection->flip(); // Collection(['bar' => 'foo', 'doe' => 'john'])
```

### get

[](#get)

Get an item from the collection

```
$collection = new Collection(['foo' => 'bar', 'john' => 'doe']);
$collection->get('foo'); // 'bar'
$collection->get('bar'); // null
$collection->get('bar', 'myDefaultValue'); // 'myDefaultValue'
```

### has

[](#has)

Determine if a key exists in the collection

```
$collection = new Collection(['foo' => 'bar', 'john' => 'doe']);
$collection->has('foo'); // true
$collection->has('bar'); // false
```

### isEmpty

[](#isempty)

Check if the collection is empty

```
$collection = new Collection(['foo' => 'bar', 'john' => 'doe']);
$collection->isEmpty(); // false
```

### keys

[](#keys)

Get all keys of the collection

```
$collection = new Collection(['foo' => 'bar', 'john' => 'doe']);
$collection->keys(); // Collection(['foo', 'john'])
```

### last

[](#last)

Search last element

```
$collection = new Collection([
    ['a' => '1', 'foo' => 'a'],
    ['a' => '2', 'foo' => 'b'],
    ['a' => '4', 'foo' => 'b'],
    ['a' => '3', 'foo' => 'c'],
]);

$collection->last(); // ['a' => '3', 'foo' => 'c']

$collection->last(function($item) {
    return $item['foo'] === 'b';
}); // ['a' => '4', 'foo' => 'b']

$collection->last(/* ... */, 'default value if not found')
```

### map

[](#map)

Applies the callback to the elements of the given arrays

```
$collection = new Collection([0, 1, 2, 3]);
$collection->map(function($item) {
    return $item + 10;
}); // Collection([10, 11, 12, 13])
```

### reduce

[](#reduce)

Reduce the array to a single value

```
function sum($carray, $item)
{
    return $carray + $item;
}

$collection = new Collection([0, 1, 2, 3]);
$collection->reduce('sum'); // 6

$collection = new Collection([
    [
        'note'  => 5,
        'coeff' => 1,
    ],
    [
        'note'  => 8,
        'coeff' => 2,
    ],
]);
$collection->reduce(function ($carry, $item) {
    return $carry + $item['note'];
}); // 13
```

### reject

[](#reject)

Create a collection without elements

```
$collection = new Collection([0, 1, 2, 3]);
$collection->reject(function($item) {
    return $item === 1;
}); // Collection([0, 2, 3])

$collection = new Collection(['foo' => 'bar', 'john' => 'doe', 'test' => 'bar']);
$collection->reject(function($item) {
    return $item === 'bar';
}); // Collection(['john' => 'doe'])
```

### sort

[](#sort)

Sort the items

```
$collection = new Collection([3, 0, 2, 1]);
$collection->sort(); // Collection([0, 1, 2, 3])

$collection = new Collection(['foo' => 'bar', 'john' => 'doe', 'test' => 'bar']);
$collection->reject(function($a, $b) {
    if ($a == $b) {
        return 0;
    }

    return ($a < $b) ? 1 : -1;
}); // Collection([3, 2, 1, 0])
```

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity31

Early-stage or recently created project

 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.

### Community

Maintainers

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

---

Top Contributors

[![jdecool](https://avatars.githubusercontent.com/u/433926?v=4)](https://github.com/jdecool "jdecool (43 commits)")

### Embed Badge

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

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

###  Alternatives

[mediawiki/babel

Users can easily indicate their language proficiency on their user page

116.4k](/packages/mediawiki-babel)

PHPackages © 2026

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