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

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

amber/collection
================

Wrapper class for working with arrays.

v0.5.3(7y ago)21.9k3GPL-3.0-or-laterPHPPHP &gt;=7.2.0

Since Apr 4Pushed 5y agoCompare

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

READMEChangelog (10)Dependencies (8)Versions (14)Used By (3)

[![GitHub last commit](https://camo.githubusercontent.com/b461e3b72a1100e1c8861863592781cc4ca9f509dfed9622384cdf5b4af14087/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f73797374656d736f6e2f636f6c6c656374696f6e2e737667)](https://camo.githubusercontent.com/b461e3b72a1100e1c8861863592781cc4ca9f509dfed9622384cdf5b4af14087/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f73797374656d736f6e2f636f6c6c656374696f6e2e737667)[![Latest Stable Version](https://camo.githubusercontent.com/604c09f283d87ba3390d6f31ae30b9577fffe39e7cce5496a272e92b83be8015/68747470733a2f2f706f7365722e707567782e6f72672f616d6265722f636f6c6c656374696f6e2f762f737461626c652e706e67)](https://packagist.org/packages/amber/collection)[![Latest Beta Version](https://camo.githubusercontent.com/b7534923db7414ed2f8631251fe302d1b125d44dd915bd4beddb189ca03fb207/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f767072652f616d6265722f636f6c6c656374696f6e2e737667)](https://packagist.org/packages/amber/collection)[![PHP from Packagist](https://camo.githubusercontent.com/884679709fe0fd36dd3535d29ea37becc4cba14d4b752601b0c8ee92ab9487cb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f616d6265722f636f6c6c656374696f6e2e737667)](https://camo.githubusercontent.com/884679709fe0fd36dd3535d29ea37becc4cba14d4b752601b0c8ee92ab9487cb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f616d6265722f636f6c6c656374696f6e2e737667)[![Build Status](https://camo.githubusercontent.com/37823b10fb278e69697c8788771643a131e89dbf9b66a7b42bdce274e52f97f0/68747470733a2f2f7472617669732d63692e6f72672f73797374656d736f6e2f636f6c6c656374696f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/systemson/collection)[![Coverage Status](https://camo.githubusercontent.com/27aa6ff9cd249bac8188172cfd6ba1b1ae467ca246129797ba678b73bdc8e019/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f73797374656d736f6e2f636f6c6c656374696f6e2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/systemson/collection?branch=master)[![CodeFactor](https://camo.githubusercontent.com/c972d7a4b7c40008e2cd420b03649ba7f4fad638411f354f28de9847af5497c7/68747470733a2f2f7777772e636f6465666163746f722e696f2f7265706f7369746f72792f6769746875622f73797374656d736f6e2f636f6c6c656374696f6e2f6261646765)](https://www.codefactor.io/repository/github/systemson/collection)[![Total Downloads](https://camo.githubusercontent.com/3b26e1520606d65eac920594d062fdbbb482b8b1d5def5b4302f4ff5e5eb5061/68747470733a2f2f706f7365722e707567782e6f72672f616d6265722f636f6c6c656374696f6e2f646f776e6c6f6164732e706e67)](https://packagist.org/packages/amber/collection)[![GitHub](https://camo.githubusercontent.com/7788937c89c3a1385c2fe0b53ba01000ad453718cae72e77dc63a7ad753fce63/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f73797374656d736f6e2f636f6c6c656374696f6e2e737667)](https://www.gnu.org/licenses/gpl.html)

Collection
==========

[](#collection)

Data structures as an alternative to the array.

Usage
-----

[](#usage)

With composer

```
composer require amber/collection

```

In your code:

```
Use Amber\Collection\Collection;
```

1. Instantiate an empty collection:

```
$collection = new Collection();
```

2. Instantiate a collection for an array:

```
$array = ['key' => 'value'];

$collection = new Collection($array);
```

3. Or use the static factory method make():

```
$collection = Collection::make();

...

$array = ['key' => 'value'];

$collection = Collection::make($array);
```

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

[](#available-methods)

- [has](#has)
- [contains](#contains)
- [hasNot](#hasnot)
- [put](#put)
- [add](#add)
- [insert](#insert)
- [update](#update)
- [push](#push)
- [pushTo](#pushto)
- [get](#get)
- [find](#find)
- [first](#first)
- [last](#last)
- [remove](#remove)
- [delete](#delete)
- [map](#map)
- [filter](#filter)
- [sort](#sort)
- [reverse](#reverse)
- [merge](#merge)
- [chunk](#chunk)
- [column](#column)
- [flip](#flip)
- [setMultiple](#setmultiple)
- [getMultiple](#getmultiple)
- [hasMultiple](#hasmultiple)
- [select](#select)
- [where](#where)
- [whereNot](#wherenot)
- [whereIn](#wherein)
- [whereNotIn](#wherenotin)
- [orderBy](#orderby)
- [groupBy](#groupby)
- [join](#join)
- [sum](#sum)
- [firstOrNew](#firstornew)
- [updateOrNew](#updateornew)

### has()

[](#has)

*Whether an item is present it the collection.*

has(string $key) : boolean

```
$collection->set('name', 'Amber');

$collection->has('name'); // returns true.

$collection->has('other'); // returns false.
```

### contains()

[](#contains)

*Alias for [has](#has).*

contains(string $key) : boolean

### hasNot()

[](#hasnot)

*Whether an item is not present it the collection.*

hasNot(string $key) : boolean

```
$collection->set('name', 'Amber');

$collection->hasNot('name'); // returns false.

$collection->hasNot('other'); // returns true.
```

### set()

[](#set)

*Sets or updates an item in the collection.*

set(string $key, mixed $value) : void

```
$collection->set('name', 'Amber');

$collection->has('name'); // returns true.
```

### put()

[](#put)

*Alias for [set](#set).*

put(string $key, mixed $value) : void

### add()

[](#add)

*Sets a new item to the collection.*

add(string $key, mixed $value) : boolean

```
$collection->add('name', 'Amber'); // returns true.

$collection->add('name', 'Amber'); // returns false, since the item already exists.
```

### insert()

[](#insert)

*Alias for [add](#add).*

insert(string $key, mixed $value) : boolean

### update()

[](#update)

*Updates an existent item in the collection.*

update(string $key, mixed $value) : boolean

```
$collection->update('name', 'Amber'); // returns false, since the item doesn't exists in the collecction

$collection->set('name', 'Amber');

$collection->update('name', 'Collection'); // returns true.
```

### push()

[](#push)

*Sets a new item at the end of the collection.*

push(mixed $value) : void

```
$collection->push('apple');
$collection->push('orange');
$collection->push('grapes');

$collection->all(); // returns ['apple', 'orange', 'grapes']
```

### pushTo()

[](#pushto)

*Push a new item at the end of a item in the collection.*

pushTo(string $key, mixed $value) : boolean

```
$collection->set('colors', []); // Sets an empty array
$collection->pushTo('colors', 'red');
$collection->pushTo('colors', 'blue');
$collection->pushTo('colors', 'green');

$collection->get('colors'); // returns ['red', 'blue', 'green']
```

### get()

[](#get)

*Gets an item from collection.*

get(string $key) : mixed|void

```
$collection->set('name', 'Amber');

$collection->get('name'); // returns 'Amber'
```

### find()

[](#find)

*Alias for [get](#get).*

find(string $key) : mixed

### first()

[](#first)

*Returns the first element of the collection.*

first() : mixed

```
$collection->push('apple');
$collection->push('orange');
$collection->push('grapes');

$collection->first(); // returns 'apple'
```

### last()

[](#last)

*Returns the last element of the collection.*

last() : mixed

```
$collection->push('apple');
$collection->push('orange');
$collection->push('grapes');

$collection->last(); // returns 'grapes'
```

### remove()

[](#remove)

*Deletes and retrives an item from collection.*

remove(string $key) : mixed

```
$collection->set('name', 'Amber');

$name = $collection->remove('name');

$collection->has('name'); // returns false

echo $name; // prints 'Amber'
```

### delete()

[](#delete)

*Deletes an item from collection.*

delete(string $key) : boolean

```
$collection->set('name', 'Amber');

$collection->delete('name'); // returns true

$collection->delete('name'); // returns false, since the items was aleady deleted
```

### map()

[](#map)

*Iterates through the collection and passes each value to the given callback.*

map(\\Closure $callback) : \\Amber\\Collection\\Base\\Collection

```
$collection->push('apple');
$collection->push('orange');
$collection->push('grapes');

$maped = $collection->map(function ($value) {
    return ucfirst($value);
});

$maped->all(); // returns ['Apple', 'Orange', 'Grapes']
```

### filter()

[](#filter)

*Returns a new filtered collection using a user-defined function.*

filter(\\Closure $callback) : \\Amber\\Collection\\Base\\Collection

```
$collection->push('apple');
$collection->push('orange');
$collection->push('grapes');

$filtered = $collection->filter(function ($value) {
    return $value == 'apple';
});

$filtered->all(); // returns ['apple']
```

### sort()

[](#sort)

*Returns a new sorted collection using a user-defined comparison function.*

sort(\\Closure $callback) : \\Amber\\Collection\\Base\\Collection

```
$collection->push(3);
$collection->push(5);
$collection->push(2);

$sorted = $collection->filter(function ($a, $b) {
    return $a  $b;
});

$sorted->all(); // returns [2, 3, 5]
```

### reverse()

[](#reverse)

*Returns a new reversed collection.*

reverse() : \\Amber\\Collection\\Base\\Collection

```
$collection->push(1);
$collection->push(2);
$collection->push(3);

$reversed = $collection->reverse();

$reversed->all() // returns [3, 2, 1]
```

### merge()

[](#merge)

*Returns a new collection merged with one or more arrays.*

merge(array $array) : \\Amber\\Collection\\Base\\Collection

```
$collection->push(1);
$collection->push(2);
$collection->push(3);

$collection->all(); // returns [1, 2, 3]

$array = [4, 5, 6];

$merged = $collection->merge($array);

$merged->all() // returns [1, 2, 3, 4, 5, 6]
```

### chunk()

[](#chunk)

*Splits an array into chunks.*

chunk(integer $size, boolean $preserve\_keys = false) : \\Amber\\Collection\\Base\\Collection

### column()

[](#column)

*Returns the values from a single column.*

column(string $column) : \\Amber\\Collection\\Base\\Collection

```
$collection->push(['id' => '1', 'color' => 'red', 'name' => 'Fire']);
$collection->push(['id' => '2', 'color' => 'blue', 'name' => 'Sea']);
$collection->push(['id' => '3', 'color' => 'green', 'name' => 'Forest']);

$colors = $collection->column('color');

$colors->all(); // returns ['red', 'blue', 'green']
```

### flip()

[](#flip)

*Exchanges all keys with their associated values.*

flip() : \\Amber\\Collection\\Base\\Collection

```
$collection->set('color' => 'red');

$fliped = $collection->flip();

$fliped->all(); // returns ['red' => 'color']
```

### setMultiple()

[](#setmultiple)

*Sets or updates an array of items in the collection, and returns true on success.*

setMultiple(array $array) : void

### getMultiple()

[](#getmultiple)

*Gets multiple items from the collection.*

getMultiple(array $array) : mixed

### hasMultiple()

[](#hasmultiple)

*Whether multiple items are present in the collection.*

hasMultiple(array $array) : boolean

### select()

[](#select)

*Returns a new Collection containing the items in the specified column(s).*

select(array|string $columns) : \\Amber\\Collection\\Base\\Collection

```
$collection->push(['id' => '1', 'color' => 'red', 'name' => 'Fire']);
$collection->push(['id' => '2', 'color' => 'blue', 'name' => 'Sea']);
$collection->push(['id' => '3', 'color' => 'green', 'name' => 'Forest']);

$items = $collection->select('id', 'color');

$items->all(); // returns [[1, 'red'], [2, 'blue'], [3, 'green']]
```

### where()

[](#where)

*Returns a new Collection containing the items in the specified column that are equal to the especified value.*

where(string $column, mixed $value) : \\Amber\\Collection\\Base\\Collection

```
$collection->push(['id' => '1', 'color' => 'red', 'name' => 'Fire']);
$collection->push(['id' => '2', 'color' => 'blue', 'name' => 'Sea']);
$collection->push(['id' => '3', 'color' => 'green', 'name' => 'Forest']);

$filtered = $collection->where('color', 'red');

$filtered->all(); // returns ['id' => '1', 'color' => 'red', 'name' => 'Fire']
```

### whereNot()

[](#wherenot)

*Returns a new Collection containing the items in the specified column that are not equal to the especified value.*

whereNot(string $column, mixed $value) : \\Amber\\Collection\\Base\\Collection

```
$collection->push(['id' => '1', 'color' => 'red', 'name' => 'Fire']);
$collection->push(['id' => '2', 'color' => 'blue', 'name' => 'Sea']);
$collection->push(['id' => '3', 'color' => 'green', 'name' => 'Forest']);

$filtered = $collection->whereNot('color', 'red');

$filtered->all();
// returns [
    ['id' => '2', 'color' => 'blue', 'name' => 'Sea'],
    ['id' => '3', 'color' => 'green', 'name' => 'Forest']
]
```

### whereIn()

[](#wherein)

*Returns a new Collection containing the items in the specified column that are equal to the especified value(s).*

whereIn(string $column, array $values = array()) : \\Amber\\Collection\\Base\\Collection

```
$collection->push(['id' => '1', 'color' => 'red', 'name' => 'Fire']);
$collection->push(['id' => '2', 'color' => 'blue', 'name' => 'Sea']);
$collection->push(['id' => '3', 'color' => 'green', 'name' => 'Forest']);

$filtered = $collection->whereIn('color', ['blue', 'green']);

$filtered->all();
// returns [
    ['id' => '2', 'color' => 'blue', 'name' => 'Sea'],
    ['id' => '3', 'color' => 'green', 'name' => 'Forest']
]
```

### whereNotIn()

[](#wherenotin)

*Returns a new Collection containing the items in the specified column that are not equal to the especified value(s).*

whereNotIn(string $column, array $values = array()) : \\Amber\\Collection\\Base\\Collection

```
$collection->push(['id' => '1', 'color' => 'red', 'name' => 'Fire']);
$collection->push(['id' => '2', 'color' => 'blue', 'name' => 'Sea']);
$collection->push(['id' => '3', 'color' => 'green', 'name' => 'Forest']);

$filtered = $collection->whereNotIn('color', ['blue', 'green']);

$filtered->all(); // returns ['id' => '1', 'color' => 'red', 'name' => 'Fire']
```

### orderBy()

[](#orderby)

*Returns a new Collection containing the items ordered by the especified column.*

orderBy(string $column, string $order = 'ASC') : \\Amber\\Collection\\Base\\Collection

```
$collection->push(['id' => '1', 'color' => 'red', 'name' => 'Fire']);
$collection->push(['id' => '2', 'color' => 'blue', 'name' => 'Sea']);
$collection->push(['id' => '3', 'color' => 'green', 'name' => 'Forest']);

$asc = $collection->orderBy('color');

$asc->all();
// returns [
    ['id' => '2', 'color' => 'blue', 'name' => 'Sea'],
    ['id' => '3', 'color' => 'green', 'name' => 'Forest'],
    ['id' => '1', 'color' => 'red', 'name' => 'Fire']
]

$desc = $collection->orderBy('color', 'desc'); // The $order param is not case sensitive.

$desc->all();
// returns [
    ['id' => '1', 'color' => 'red', 'name' => 'Fire'],
    ['id' => '3', 'color' => 'green', 'name' => 'Forest'],
    ['id' => '2', 'color' => 'blue', 'name' => 'Sea']
]
```

### groupBy()

[](#groupby)

*Returns a new Collection grouped by the specified column.*

groupBy(string $column) : \\Amber\\Collection\\Base\\Collection

```
$collection->push(['id' => '1', 'color' => 'red', 'name' => 'Fire']);
$collection->push(['id' => '2', 'color' => 'blue', 'name' => 'Sea']);
$collection->push(['id' => '3', 'color' => 'green', 'name' => 'Forest']);

$grouped = $collection->groupBy('color');

$grouped->all();
// returns [
    'red' => ['id' => '1', 'color' => 'red', 'name' => 'Fire'],
    'blue' => ['id' => '2', 'color' => 'blue', 'name' => 'Sea'],
    'green' => ['id' => '3', 'color' => 'green', 'name' => 'Forest']
]
```

### join()

[](#join)

*Returns a new Collection joined by the specified column.*

join(array $array, string $pkey, string $fkey) : \\Amber\\Collection\\Base\\Collection

### sum()

[](#sum)

*Calculates the sum of values in the collection.*

sum(string $column = null) : integer

```
$collection->push(2);
$collection->push(5);
$collection->push(3);

$collection->sum(); // returns 10
```

Or you can sum a specific colum

```
$collection->push(['id' => 1, 'name' => 'red', 'quantity' => 2]);
$collection->push(['id' => 2, 'name' => 'blue', 'quantity' => 5]);
$collection->push(['id' => 3, 'name' => 'green', 'quantity' => 3]);

$collection->sum('quantity'); // returns 10

```

### firstOrNew()

[](#firstornew)

*Gets the first item of the Collection or adds and returns a new one.*

firstOrNew(string $key, mixed $value) : mixed

### updateOrNew()

[](#updateornew)

*Updates an item from the Collection or adds a new one.*

updateOrNew(string $key, mixed $value) : mixed

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.6% 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 ~24 days

Recently: every ~55 days

Total

13

Last Release

2307d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1912ffd7709c7c3d7498921fd3080e23589e5cee9fb84a4d3831126739ac85b5?d=identicon)[systemson](/maintainers/systemson)

---

Top Contributors

[![systemson](https://avatars.githubusercontent.com/u/32528630?v=4)](https://github.com/systemson "systemson (159 commits)")[![devilu](https://avatars.githubusercontent.com/u/25694532?v=4)](https://github.com/devilu "devilu (6 commits)")[![ivvanare](https://avatars.githubusercontent.com/u/32280279?v=4)](https://github.com/ivvanare "ivvanare (3 commits)")

---

Tags

arraysbagcollectiondata-structuresmapphpsetvector

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[malios/php-to-ascii-table

A PHP library to generate plain text tables.

30118.8k1](/packages/malios-php-to-ascii-table)[samsara/fermat

A library providing math and statistics operations for numbers of arbitrary size.

653.1k3](/packages/samsara-fermat)[shrink0r/workflux

Finite state machine for php.

375.6k1](/packages/shrink0r-workflux)

PHPackages © 2026

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