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

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

arsalanthange/php-collections
=============================

A simple PHP collection class that makes your life easier to work with arrays. Inspired from Laravel Collections.

v1.0.0(7y ago)151MITPHPPHP &gt;=7.1

Since Apr 21Pushed 7y ago1 watchersCompare

[ Source](https://github.com/ArsalanThange/php-collections)[ Packagist](https://packagist.org/packages/arsalanthange/php-collections)[ RSS](/packages/arsalanthange-php-collections/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (2)Used By (0)

About PHP Collections
---------------------

[](#about-php-collections)

This library converts your arrays into Collections. It comes with various built in functions to manipulate and work with arrays. The class is immutable, meaning every Collection method you execute will return an entirely new Collection instance.

Usage
-----

[](#usage)

#### Converting Array into Collection

[](#converting-array-into-collection)

```
// Array which is to be converted to a Collection
$array = [5, 'a', 't', 2, 7];

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

#### Sorting the Collection

[](#sorting-the-collection)

```
//Sort in Ascending Order
$collection->sort()->all();

//Output: ['a', 't', 2, 5, 7]

//Sort in Descending Order
$collection->sort('DESC')->all();

//Output: [7, 5, 2, 't', 'a']
```

#### Paginate the Collection

[](#paginate-the-collection)

Note: This method does not return a `Collection` instance. The `paginate` method accepts 2 parameters.

```
$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

$collection = new Collection($array);

//Page number
$page = 1;

//Number of elements to be displayed on the page
$count = 4

//Paginate the collection
$collection->paginate($page, $count);

/*Output: [
    'total' => 20,
    'current_page' => 1,
    'total_pages' => 5,
    'from' => 1,
    'to' => 4,
    'data' => [1, 2, 3, 4]
]
*/
```

If you require elements on the next page simply increment the page value. Note: The `paginate` method should run on the initial collection for page increments and not on the newly returned instance.

#### Filter the Collection

[](#filter-the-collection)

Filtering works on all the basic operations such as `=`, `!=`, `>`, `=` and ` 5, 'bananas' => 3, 'oranges' => 2],
    ['apples' => 10, 'bananas' => 5, 'oranges' => 1],
    ['apples' => 15, 'bananas' => 10, 'oranges' => 6]
];

$collection = new Collection($array);

//Filter the collection
$collection->where('apples', '>', 5)->all();

/*
Output: [
    ['apples' => 10, 'bananas' => 5, 'oranges' => 1],
    ['apples' => 15, 'bananas' => 10, 'oranges' => 6]
]
*/
```

You can chain the `where` method to apply multiple filters.

```
//Filter the collection
$collection->where('apples', '>', 5)->where('bananas', '=', 10)->all();

/*
Output: [
    ['apples' => 15, 'bananas' => 10, 'oranges' => 6]
]
*/
```

#### Count the number of elements in the Collection

[](#count-the-number-of-elements-in-the-collection)

```
$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$collection = new Collection($array);

//Count the number of elements
$collection->count();

//Output: 10
```

#### Reset Array keys

[](#reset-array-keys)

The `values` method resets your array keys.

```
$array = [
    'first_basket' => ['apples' => 5, 'bananas' => 3, 'oranges' => 2],
    'second_basket' => ['apples' => 10, 'bananas' => 5, 'oranges' => 1],
    'third_basket' => ['apples' => 15, 'bananas' => 10, 'oranges' => 6]
];

$collection = new Collection($array);

$collection->values()->all();

/*Output: [
    ['apples' => 5, 'bananas' => 3, 'oranges' => 2],
    ['apples' => 10, 'bananas' => 5, 'oranges' => 1],
    ['apples' => 15, 'bananas' => 10, 'oranges' => 6]
]
*/
```

#### Get value of a specified key

[](#get-value-of-a-specified-key)

The `get` method returns the value of the specified key. Returns `null` if the key does not exist and if no default value is specified.

```
$array = [
    'first_basket' => ['apples' => 5, 'bananas' => 3, 'oranges' => 2],
    'second_basket' => ['apples' => 10, 'bananas' => 5, 'oranges' => 1],
    'third_basket' => ['apples' => 15, 'bananas' => 10, 'oranges' => 6]
];

$collection = new Collection($array);

$collection->get('first_basket');

// Output: ['apples' => 5, 'bananas' => 3, 'oranges' => 2]

$collection->get('fourth_basket');

// Output: null

//Second parameter is the default value if the key does not exist
$collection->get('fourth_basket', 'No fruits here!');

// Output: No fruits here!
```

#### Sum elements in the collection

[](#sum-elements-in-the-collection)

```
$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$collection = new Collection($array);

//Sum elments
$collection->sum();

//Output: 55
```

#### Average elements in the collection

[](#average-elements-in-the-collection)

```
$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$collection = new Collection($array);

//Average elements
$collection->avg();

//Output: 5.5
```

#### Join elements in the collection into a string

[](#join-elements-in-the-collection-into-a-string)

```
$array = [1, 2, 3, 4];

$collection = new Collection($array);

//Join elements
$collection->implode();

//Output: 1,2,3,4
```

If you wish to specify the `glue` for implode you can pass the value in `implode` method.

```
//Join elements
$collection->implode('-');

//Output: 1-2-3-4
```

If the collection contains arrays or objects, you must pass the `key` of the attributes you wish to join, and the `glue` you wish to place between the values.

```
$array = [
    [ 'foo' => 1, 'bar' => 2],
    [ 'foo' => 3, 'bar' => 4],
    [ 'foo' => 5, 'bar' => 6],
];

$collection = new Collection($array);

//Join elements
$collection->implode(',', 'foo');

//Output: 1,3,5
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

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

2581d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/98d2fc803e24448e5e84fc09b3306aafa294e0665b738b4aca0c41b395da3de2?d=identicon)[ArsalanThange](/maintainers/ArsalanThange)

---

Tags

phparraycollection

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/arsalanthange-php-collections/health.svg)](https://phpackages.com/packages/arsalanthange-php-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)[iteks/laravel-enum

A comprehensive Laravel package providing enhanced enum functionalities, including attribute handling, select array conversions, and fluent facade interactions for robust enum management in Laravel applications.

2516.7k](/packages/iteks-laravel-enum)

PHPackages © 2026

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