PHPackages                             selvinortiz/collective - 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. selvinortiz/collective

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

selvinortiz/collective
======================

Simple collection implementation on top of native arrays

0.5.0(9y ago)41423MITPHPPHP &gt;=5.4.0CI failing

Since Jun 12Pushed 5y ago1 watchersCompare

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

READMEChangelogDependencies (1)Versions (7)Used By (0)

[![Collective](Collective.png)](Collective.png)

[![Build Status](https://camo.githubusercontent.com/9a6edad777fc1b793ea172a9e4d207214fd6d3baad3d9a4695a46d875c57a373/68747470733a2f2f7472617669732d63692e6f72672f73656c76696e6f7274697a2f636f6c6c6563746976652e706e67)](https://travis-ci.org/selvinortiz/collective)[![Total Downloads](https://camo.githubusercontent.com/18a2b42dcf57a955bfdf1de675d143449be6517b27541d22e51088931b8c6a90/68747470733a2f2f706f7365722e707567782e6f72672f73656c76696e6f7274697a2f636f6c6c6563746976652f642f746f74616c2e706e67)](https://packagist.org/packages/selvinortiz/collective)[![Latest Stable Version](https://camo.githubusercontent.com/9a790136e27f189ef2ba72ebbb8010a16e12d93bc597fa5c55092c4304b099ef/68747470733a2f2f706f7365722e707567782e6f72672f73656c76696e6f7274697a2f636f6c6c6563746976652f762f737461626c652e706e67)](https://packagist.org/packages/selvinortiz/collective)

Description
-----------

[](#description)

> *Collective* is a lightweight library that allows you to interact with native arrays in a more flexible and elegant way. It is inpired by *Laravel Collections* and focused on performance.

### Requirements

[](#requirements)

- PHP 5.5+
- [Composer](http://getcomposer.org) and [selvinortiz/collective](https://packagist.org/packages/selvinortiz/collective)

### Install

[](#install)

```
composer require selvinortiz/collective
```

### Test

[](#test)

```
sh spec.sh
```

### Usage

[](#usage)

```
// 1. Create a new instance of Collective
// 2. Give it an (optional) input array
// 3. Call methods on it
$input = ['name' => 'Collective', 'release' => 'alpha'];

(new Collective($input))->get('name');
// 'Collective'
```

### Reference

[](#reference)

---

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

[](#getkey-default--null)

```
$input = [
    'user' => [
        'name'   => 'Brad',
        'salary' => '100000'
    ]
];

(new Collective($input))->get('users.name');
// 'Brad'
```

#### `set($key, $value)`

[](#setkey-value)

```
(new Collection())->set('user.name', 'Matt')->toArray();
// ['user' => ['name' => 'Matt']]
```

#### `count()`

[](#count)

```
$input = [0, 1, 2, 3, 4, 5];

(new Collective($input))->count();
// 6
```

#### `first(callable $callback = null, $default = null)`

[](#firstcallable-callback--null-default--null)

```
$input      = [128, 256, 512, 'Brad', 'Brandon', 'Matt'];
$collective = new Collective($input);

$collective->first();
// 128

$collective->first(function($value)
{
    return strpos($value, 'Bra') !== false;
});
// Brad
```

#### `last(callable $callback = null, $default = null)`

[](#lastcallable-callback--null-default--null)

```
$input      = [128, 256, 512 'Brad', 'Brandon', 'Matt'];
$collective = new Collective($input);

$collective->last();
// 'Matt'

$collective->last(function($value)
{
    return strpos($value, 'Bra') !== false;
});
// Brandon
```

#### `map(callable $callback, array $default = [])`

[](#mapcallable-callback-array-default--)

Applies your *callable* to each item in the collection

```
$input = [128, 256, 512 'Brad', 'Brandon', 'Matt'];

(new Collective($input))->map(function($value)
{
    return is_string($value) ? '- '.$value : $value;
})->toArray();
// 128, 256, 512, '- Brad', '- Brandon', '- Matt'
```

#### `filter(callable $callback = null, array $default = null)`

[](#filtercallable-callback--null-array-default--null)

Filters each item in the collection using your own *callable*

```
$input = [128, 256, 512 'Brad', 'Brandon', 'Matt'];
(new Collective($input))->filter(function($value)
{
    return is_numeric($value);
})->toArray();
// 128, 256, 512
```

#### `reduce(callable $callback, $initial = null)`

[](#reducecallable-callback-initial--null)

Reduces a collection to a single value

```
$input    = [
    ['name' => 'Brad', 'salary' => 100000, 'type' => 'yearly'],
    ['name' => 'Brandon', 'salary' => 250000, 'type' => 'yearly']
];

$callback = function ($value, $carry) {
    return $carry + $value['salary'];
};

(new Collective($input))->reduce($callback);
// 350000
```

#### `reverse()`

[](#reverse)

```
(new Collective([128, 256, 512]))->reverse()->toArray();
// [512, 256, 128]
```

#### `flatten($depth = INF)`

[](#flattendepth--inf)

```
$input = [
    'level1' => [
        'name'   => 'Level 1',
        'level2' => [
            'name'   => 'Level 2',
            'level3' => [
                'name' => 'Level 3'
            ]
        ]
    ]
];

(new Collective($input))->flatten()->toArray();
// ['Level 1', 'Level 2', 'Level 3']
```

#### `then(callable $callback)`

[](#thencallable-callback)

Chains functions not defined by the *collection* without breaking the *pipe*

```
function filterToStrings($collective) {
    return $collective->filter(function ($value) { return is_string($value); });
}

function fourCharsOnly($collective) {
    return $collective->filter(function ($value) { return strlen($value) == 4; });
}

$collective->then('filterToStrings')->then('filterToLength')->toArray();
// 'Brad', 'Matt'
```

---

### License

[](#license)

**Collective** is open source software licensed under the **MIT License**

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 91.7% 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 ~1 days

Total

5

Last Release

3614d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/93d52d70a5bafe64daf0512be1f3473bae56fa5bfb255258e30d6de3dcfcd170?d=identicon)[selvinortiz](/maintainers/selvinortiz)

---

Top Contributors

[![selvinortiz](https://avatars.githubusercontent.com/u/1922523?v=4)](https://github.com/selvinortiz "selvinortiz (11 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

---

Tags

arraycollectioncollective

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/selvinortiz-collective/health.svg)

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

###  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)[athari/yalinqo

YaLinqo, a LINQ-to-objects library for PHP

4561.2M5](/packages/athari-yalinqo)[yansongda/supports

common components

211.4M31](/packages/yansongda-supports)[armincms/json

A Laravel Nova field.

25149.4k3](/packages/armincms-json)[graze/sort

A collection of array sorting transforms and functions

12289.6k2](/packages/graze-sort)[graze/data-structure

Data collections and containers

12287.4k8](/packages/graze-data-structure)

PHPackages © 2026

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