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

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

werx/collections
================

Base class for working with collections of things

0.3.0(12y ago)02.3kMITPHPPHP &gt;= 5.4

Since Apr 30Pushed 12y ago1 watchersCompare

[ Source](https://github.com/werx/collections)[ Packagist](https://packagist.org/packages/werx/collections)[ RSS](/packages/werx-collections/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (2)Versions (5)Used By (0)

Collection
==========

[](#collection)

Base class for working with collections of things.

[![Build Status](https://camo.githubusercontent.com/35bcdbe68972044166c00714183def1a0c14db5d46500aafa64b945a516ca3f5/68747470733a2f2f7472617669732d63692e6f72672f776572782f636f6c6c656374696f6e732e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/werx/collections) [![Total Downloads](https://camo.githubusercontent.com/13c2d243157b989c23ba1093fb9af9e85d52325390b1beae81e91b3aa612284a/68747470733a2f2f706f7365722e707567782e6f72672f776572782f636f6c6c656374696f6e732f646f776e6c6f6164732e706e67)](https://packagist.org/packages/werx/collections) [![Latest Stable Version](https://camo.githubusercontent.com/ce3442ea5ec705e7dedd0ce884609372978c9f4624a6c85a7ee3255f804f5290/68747470733a2f2f706f7365722e707567782e6f72672f776572782f636f6c6c656374696f6e732f762f737461626c652e706e67)](https://packagist.org/packages/werx/collections)

Usage
-----

[](#usage)

You can either create instances of the Collection object...

```
$foo = new \werx\Collections\Collection;
```

Or create a class that extends it.

```
class Foo extends \werx\Collections\Collection
{

}

$foo = new Foo();
```

### Fill the collection in the constructor.

[](#fill-the-collection-in-the-constructor)

```
$foo = new new \werx\Collections\Collection(['foo' => 'Foo', 'bar' => 'bar']);
```

### Add multiple items to collection by passing an array to the `set()` method.

[](#add-multiple-items-to-collection-by-passing-an-array-to-the-set-method)

```
$foo = new Foo();
$foo->set(['foo' => 'Foo', 'bar' => 'bar']);
```

### How many items in the collection?

[](#how-many-items-in-the-collection)

```
var_dump($foo->count());
# OR
var_dump(count($foo)); // The Collection class implements the Countable() interface.
// 2
```

### Does the collection have a key named 'foo'? (yes)

[](#does-the-collection-have-a-key-named-foo-yes)

```
var_dump($foo->has('foo'));
// true
```

### Does the collection have a key named 'x'? (no)

[](#does-the-collection-have-a-key-named-x-no)

```
var_dump($foo->has('x'));
// false
```

### Get the value of the key named 'foo' from the collection.

[](#get-the-value-of-the-key-named-foo-from-the-collection)

```
var_dump($foo->get('foo'));
// Foo
```

### Return default null if key doesn't exist.

[](#return-default-null-if-key-doesnt-exist)

```
var_dump($foo->get('x'));
// null
```

### Set a different default value if key doesn't exist.

[](#set-a-different-default-value-if-key-doesnt-exist)

```
var_dump($foo->get('x', 'no'));
// no
```

### Set a key/value.

[](#set-a-keyvalue)

```
$foo->set('name', 'Josh');
```

### Add an item to the collection without a specific key.

[](#add-an-item-to-the-collection-without-a-specific-key)

```
$foo->add('Josh');
```

### Add an array to the collection without a specific key.

[](#add-an-array-to-the-collection-without-a-specific-key)

```
$foo->add(['name' => 'Josh', 'location' => 'Arkansas']);
```

### Remove a key from the collection.

[](#remove-a-key-from-the-collection)

```
$foo->remove('name');
```

### Get all keys in the collection.

[](#get-all-keys-in-the-collection)

```
$foo->all();
```

### OR

[](#or)

```
$foo->toArray();
```

### Convert the collection to json.

[](#convert-the-collection-to-json)

```
var_dump($foo->toJson());
// {"foo":"Foo","bar":"Bar"}
```

### Casting the collection to a string will also return json.

[](#casting-the-collection-to-a-string-will-also-return-json)

```
var_dump((string) $foo);
// {"foo":"Foo","bar":"Bar"}
```

### Empty the collection.

[](#empty-the-collection)

```
$foo->clear();
```

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

[](#installation)

Installation of this package is easy with Composer. If you aren't familiar with the Composer Dependency Manager for PHP, [you should read this first](https://getcomposer.org/doc/00-intro.md).

### composer.json

[](#composerjson)

```
"require": {
	"werx/collections": "dev-master"
}
```

Contributing
------------

[](#contributing)

### Unit Testing

[](#unit-testing)

```
$ vendor/bin/phpunit

```

### Coding Standards

[](#coding-standards)

This library uses [PHP\_CodeSniffer](http://www.squizlabs.com/php-codesniffer) to ensure coding standards are followed.

I have adopted the [PHP FIG PSR-2 Coding Standard](http://www.php-fig.org/psr/psr-2/) EXCEPT for the tabs vs spaces for indentation rule. PSR-2 says 4 spaces. I use tabs. No discussion.

To support indenting with tabs, I've defined a custom PSR-2 ruleset that extends the standard [PSR-2 ruleset used by PHP\_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/PSR2/ruleset.xml). You can find this ruleset in the root of this project at PSR2Tabs.xml

Executing the codesniffer command from the root of this project to run the sniffer using these custom rules.

```
$ ./codesniffer

```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

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

Every ~5 days

Total

4

Last Release

4386d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1cda8a02682a999e89bc2443c6c169b1dc9228cc4709d0b5fad642dc2a151141?d=identicon)[joshmoody](/maintainers/joshmoody)

---

Top Contributors

[![joshmoody](https://avatars.githubusercontent.com/u/1504862?v=4)](https://github.com/joshmoody "joshmoody (10 commits)")

---

Tags

arraycollection

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  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)
