PHPackages                             seboettg/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. [Queues &amp; Workers](/categories/queues)
4. /
5. seboettg/collection

ActiveLibrary[Queues &amp; Workers](/categories/queues)

seboettg/collection
===================

Collection is a set of useful PHP wrapper classes for arrays, similar to Java Collection. Contains ArrayList, Stack, Queue.

v4.0.0(2y ago)91.2M—7.3%42MITPHPPHP &gt;=7.4

Since Aug 1Pushed 2y ago3 watchersCompare

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

READMEChangelog (10)Dependencies (2)Versions (22)Used By (2)

[![PHP](https://camo.githubusercontent.com/eb6a51d87bff96f720a40442f90376345af1788b5abd314d94d302170e5b0d54/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d2533453d372e342d677265656e2e7376673f7374796c653d666c6174)](http://docs.php.net/manual/en/migration74.new-features.php)[![Total Downloads](https://camo.githubusercontent.com/e9e8fc32bbc2ced902b99df0365a278dc56453477e18bf0506d43ab93dbc9d05/68747470733a2f2f706f7365722e707567782e6f72672f7365626f657474672f636f6c6c656374696f6e2f646f776e6c6f616473)](https://packagist.org/packages/seboettg/collection/stats)[![License](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](https://github.com/seboettg/Collection/blob/master/LICENSE)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/824887acc6eb3f8b43159e8f13c31c9988485414f8cf04ad4da7617079b22654/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7365626f657474672f436f6c6c656374696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/seboettg/Collection/?branch=master)[![Build Status](https://camo.githubusercontent.com/2bbeaa3f97b8d8e76ec9160c4e5afb36abdd8e3a23b35db5232392944de3b096/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7365626f657474672f436f6c6c656374696f6e2f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/seboettg/Collection/build-status/master)[![Code Coverage](https://camo.githubusercontent.com/e9bae98fb538be94e5398a6b95782b6ab6e0243fd6212a25976d847caffcc15e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7365626f657474672f436f6c6c656374696f6e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/seboettg/Collection/code-structure/master)[![Code Intelligence Status](https://camo.githubusercontent.com/24b97a00e1634f3c9f044c7ddd1603aa6244819ee999ec882fb30bccbed659fc/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7365626f657474672f436f6c6c656374696f6e2f6261646765732f636f64652d696e74656c6c6967656e63652e7376673f623d6d6173746572)](https://scrutinizer-ci.com/code-intelligence)

Collection
==========

[](#collection)

Collection is a set of useful wrapper classes for arrays, similar to Java's or Kotlin's collection packages.

Table of Contents
-----------------

[](#table-of-contents)

1. [Versions](#versions)
2. [Installing Collection](#install)
3. [Lists](#lists)
    1. [Getting started](#lists-getting-started)
    2. [Iterate over lists](#lists-iterate)
    3. [List operations](#lists-operations)
    4. [Map elements](#lists-map)
    5. [Filter elements](#lists-filter)
    6. [Logical operations](#lists-logical-operations)
    7. [forEach](#lists-foreach)
    8. [sorting](#lists-sorting)
4. [Maps](#maps)
    1. [Getting started](#maps-getting-started)
    2. [Access elements](#maps-access-elements)
    3. [Manipulate](#maps-manipulate)
    4. [Map elements](#maps-map)
    5. [Filter map entries](#maps-filter)
5. [Combining Lists and Maps](#lists-maps)
6. [Stack](#stack)
7. [Queue](#queue)
8. [Contribution](#contribution)

Versions
--------

[](#versions)

Since [Version 4.0](https://github.com/seboettg/Collection/releases/tag/v4.0.0) you need PHP 7.4 or higher to use this library. Since [Version 2.1](https://github.com/seboettg/Collection/releases/tag/v2.1.0) you need PHP 7.1 to use Collection library. Previous versions are running from PHP 5.6 upwards.

Installing Collection
---------------------

[](#installing-collection)

The recommended way to install Collection is through [Composer](http://getcomposer.org).

```
# Install Composer
curl -sS https://getcomposer.org/installer | php
```

Next, run the Composer command to install the latest stable version of Collection:

```
php composer.phar require seboettg/collection
```

After installing, you need to require Composer's autoloader:

```
require 'vendor/autoload.php';
```

You can then later update Collection using composer:

```
composer.phar update
```

Lists
-----

[](#lists)

List is an ordered collection with access to elements by indices – integer numbers that reflect their position. Elements can occur more than once in a list. In other words: a list can contain any number of equal objects or occurrences of a single object. Two lists are considered equal if they have the same sizes and structurally equal elements at the same positions.

Lists are completely new implemented for version 4.0. The handling is much more oriented on a functional approach. Further more methods for associative arrays are moved to map.

### Getting started

[](#getting-started)

```
use function Seboettg\Collection\Lists\listOf;
use function Seboettg\Collection\Lists\listFromArray;
//create a simple list
$list = listOf("a", "b", "c", "d");
print_r($list);
```

Output

```
Seboettg\Collection\Lists\ListInterface@anonymous Object
(
    [array:Seboettg\Collection\Lists\ListInterface@anonymous:private] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

    [offset:Seboettg\Collection\Lists\ListInterface@anonymous:private] => 0
)

```

You also create a list from an existing array

```
$array = ["d", "e", "f"];
$otherList = listFromArray($array);
```

Output

```
Seboettg\Collection\Lists\ListInterface@anonymous Object
(
    [array:Seboettg\Collection\Lists\ListInterface@anonymous:private] => Array
        (
            [0] => d
            [1] => e
            [2] => f
        )

    [offset:Seboettg\Collection\Lists\ListInterface@anonymous:private] => 0
)

```

As you may notice, this will reset the array keys

You can also create an empty List:

```
use function Seboettg\Collection\Lists\emptyList;
$emptyList = emptyList();
echo $emptyList->count();
```

output

```
0

```

### Iterate over lists

[](#iterate-over-lists)

```
foreach ($list as $key => $value) {
    echo "[".$key."] => ".$value."\n";
}
```

Output:

```
[0] => a
[1] => b
[2] => c

```

or

```
for ($i = 0; $i < $otherList->count(); ++$i) {
    echo $otherList->get($i) . " ";
}
```

output

```
d e f

```

### List operations

[](#list-operations)

You may add the elements of another list to a list by using `plus`:

```
$newList = $list->plus($otherList);
print_r($newList);
```

output

```
Seboettg\Collection\Lists\ListInterface@anonymous Object
(
    [array:Seboettg\Collection\Lists\ListInterface@anonymous:private] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
            [4] => e
            [5] => f
        )
    [offset:Seboettg\Collection\Lists\ListInterface@anonymous:private] => 0
)

```

The same operation is applicable with arrays, with the same result:

```
$newList = $list->plus($array);
```

You can also subtract the elements of another list or any `iterable` using `minus`:

```
$subtract = $newList->minus($list);
print_r($subtract);
```

output

```
Seboettg\Collection\Lists\ListInterface@anonymous Object
(
    [array:Seboettg\Collection\Lists\ListInterface@anonymous:private] => Array
        (
            [0] => d
            [1] => e
            [2] => f
        )

    [offset:Seboettg\Collection\Lists\ListInterface@anonymous:private] => 0
)

```

To get the intersection of two lists (or an iterable), you can use the `intersect` method:

```
$intersection = $newList->intersect(listOf("b", "d", "f", "h", "i"));
print_r($intersection);
```

output

```
Seboettg\Collection\Lists\ListInterface@anonymous Object
(
    [array:Seboettg\Collection\Lists\ListInterface@anonymous:private] => Array
        (
            [0] => b
            [1] => d
            [2] => f
        )

    [offset:Seboettg\Collection\Lists\ListInterface@anonymous:private] => 0
)

```

To get a list containing distinct elements, use `distinct`:

```
$list = listOf("a", "b", "a", "d", "e", "e", "g")
print_r($list->distinct());
```

output

```
Seboettg\Collection\Lists\ListInterface@anonymous Object
(
    [array:Seboettg\Collection\Lists\ListInterface@anonymous:private] => Array
        (
            [0] => a
            [1] => b
            [2] => d
            [3] => e
            [4] => g
        )

    [offset:Seboettg\Collection\Lists\ListInterface@anonymous:private] => 0
)

```

### Map all elements of a list

[](#map-all-elements-of-a-list)

If you need to modify all elements in a list, you can do it easily by using the `map` method:

```
$list = listOf(1, 2, 3, 4, 5);
$cubicList = $list->map(fn ($i) => $i * $i * $i);
//result of $cubicList: 1, 8, 27, 64, 125
```

There is also a `mapNotNull` method that eliminates `null` values from the result:

```
function divisibleByTwoOrNull(int $number): ?int {
    return $item % 2 === 0 ? $item : null;
}

listOf(0, 1, 2, 3, 4, 5)
    ->map(fn (int $number): ?int => divisibleByTwoOrNull($number));
//result: 0, 2, 4
```

### Filter elements in a list

[](#filter-elements-in-a-list)

The `filter` method returns a list containing only elements matching the given predicate.

```
$list = listOf("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"):
$listOfCharactersThatAsciiNumbersIsOdd = $list
    ->filter(fn($letter) => ord($letter) % 2 !== 0);
//result of $listOfCharactersTharOrderNumbersAreOdd: "a", "c", "e", "g", "i"
```

### Logical operations

[](#logical-operations)

With the methods `any` and `all` you can check whether all elements (all) or at least one element (any) match a predicate.

```
$list = listOf("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"):
$list->all(fn($letter) => ord($letter) % 2 !== 0); // false
$list->any(fn($letter) => ord($letter) % 2 !== 0); // true

$list->all(fn($letter) => ord($letter) % 1 !== 0); // true
$list->any(fn($letter) => $letter === "z"); // false, since no character in the list is a 'z'
```

### forEach method

[](#foreach-method)

With the forEach method you can apply a closure or lambda functions on each element.

```
$list = listOf("a", "b", "c");
$list->forEach(fn (string $item) => print($item . PHP_EOL));
```

output:

```
a
b
c

```

### Sorting a list

[](#sorting-a-list)

Implement the Comparable interface

```
