{"id":21670761,"url":"https://github.com/seboettg/collection","last_synced_at":"2026-03-15T08:42:19.260Z","repository":{"id":10096318,"uuid":"64466880","full_name":"seboettg/Collection","owner":"seboettg","description":"Collection is a set of useful PHP wrapper classes for arrays, similar to Java Collection. Contains List, Map, Stack, Queue.","archived":false,"fork":false,"pushed_at":"2023-12-07T21:53:37.000Z","size":370,"stargazers_count":9,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-09-29T09:02:03.553Z","etag":null,"topics":["array","arraylist","basic-data-structures","collections","comparable","comparable-interface","comparator","datastructures","filter","list","lists","map","oop","php","queue","sort","stack"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/seboettg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-07-29T09:04:58.000Z","updated_at":"2023-03-11T12:58:31.000Z","dependencies_parsed_at":"2024-01-16T12:01:00.725Z","dependency_job_id":null,"html_url":"https://github.com/seboettg/Collection","commit_stats":{"total_commits":107,"total_committers":7,"mean_commits":"15.285714285714286","dds":0.6168224299065421,"last_synced_commit":"4dbffe7c12825dc3cbb2394e1adc71e34aa0b6af"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"purl":"pkg:github/seboettg/Collection","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seboettg%2FCollection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seboettg%2FCollection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seboettg%2FCollection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seboettg%2FCollection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/seboettg","download_url":"https://codeload.github.com/seboettg/Collection/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seboettg%2FCollection/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279019558,"owners_count":26086750,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-14T02:00:06.444Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array","arraylist","basic-data-structures","collections","comparable","comparable-interface","comparator","datastructures","filter","list","lists","map","oop","php","queue","sort","stack"],"created_at":"2024-11-25T12:35:12.228Z","updated_at":"2026-03-15T08:42:19.187Z","avatar_url":"https://github.com/seboettg.png","language":"PHP","readme":"[![PHP](https://img.shields.io/badge/PHP-%3E=7.4-green.svg?style=flat)](http://docs.php.net/manual/en/migration74.new-features.php)\n[![Total Downloads](https://poser.pugx.org/seboettg/collection/downloads)](https://packagist.org/packages/seboettg/collection/stats) \n[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/seboettg/Collection/blob/master/LICENSE)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/seboettg/Collection/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/seboettg/Collection/?branch=master)\n[![Build Status](https://scrutinizer-ci.com/g/seboettg/Collection/badges/build.png?b=master)](https://scrutinizer-ci.com/g/seboettg/Collection/build-status/master)\n[![Code Coverage](https://scrutinizer-ci.com/g/seboettg/Collection/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/seboettg/Collection/code-structure/master)\n[![Code Intelligence Status](https://scrutinizer-ci.com/g/seboettg/Collection/badges/code-intelligence.svg?b=master)](https://scrutinizer-ci.com/code-intelligence)\n\n# Collection\n\nCollection is a set of useful wrapper classes for arrays, similar to Java's or Kotlin's collection packages. \n\n## Table of Contents\n1. [Versions](#versions)\n2. [Installing Collection](#install)\n3. [Lists](#lists)\n   1. [Getting started](#lists-getting-started)\n   2. [Iterate over lists](#lists-iterate)\n   3. [List operations](#lists-operations)\n   4. [Map elements](#lists-map)\n   5. [Filter elements](#lists-filter)\n   6. [Logical operations](#lists-logical-operations)\n   7. [forEach](#lists-foreach)\n   8. [sorting](#lists-sorting)\n4. [Maps](#maps)\n   1. [Getting started](#maps-getting-started)\n   2. [Access elements](#maps-access-elements)\n   3. [Manipulate](#maps-manipulate)\n   4. [Map elements](#maps-map)\n   5. [Filter map entries](#maps-filter)\n5. [Combining Lists and Maps](#lists-maps)\n6. [Stack](#stack)\n7. [Queue](#queue)\n8. [Contribution](#contribution)\n\n\n\u003ca name=\"versions\"/\u003e\n\n## Versions\nSince [Version 4.0](https://github.com/seboettg/Collection/releases/tag/v4.0.0) you need PHP 7.4 or higher to use this library.\nSince [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.\n\n\n\u003ca name=\"install\"/\u003e\n\n## Installing Collection\n\nThe recommended way to install Collection is through\n[Composer](http://getcomposer.org).\n\n```bash\n# Install Composer\ncurl -sS https://getcomposer.org/installer | php\n```\n\nNext, run the Composer command to install the latest stable version of Collection:\n\n```bash\nphp composer.phar require seboettg/collection\n```\n\nAfter installing, you need to require Composer's autoloader:\n\n```php\nrequire 'vendor/autoload.php';\n```\n\nYou can then later update Collection using composer:\n\n ```bash\ncomposer.phar update\n ```\n\n\u003ca name=\"lists\"/\u003e\n\n## Lists\n\nList is an ordered collection with access to elements by indices – integer numbers that reflect their position. Elements\ncan occur more than once in a list. In other words: a list can contain any number of equal objects or occurrences of a \nsingle object. Two lists are considered equal if they have the same sizes and structurally equal elements at the same \npositions.\n\nLists are completely new implemented for version 4.0. The handling is much more oriented on a functional approach. \nFurther more methods for associative arrays are moved to map.\n\n\u003ca name=\"lists-getting-started\"/\u003e\n\n### Getting started\n\n```php\nuse function Seboettg\\Collection\\Lists\\listOf;\nuse function Seboettg\\Collection\\Lists\\listFromArray;\n//create a simple list\n$list = listOf(\"a\", \"b\", \"c\", \"d\");\nprint_r($list);\n```\nOutput \n```\nSeboettg\\Collection\\Lists\\ListInterface@anonymous Object\n(\n    [array:Seboettg\\Collection\\Lists\\ListInterface@anonymous:private] =\u003e Array\n        (\n            [0] =\u003e a\n            [1] =\u003e b\n            [2] =\u003e c\n        )\n\n    [offset:Seboettg\\Collection\\Lists\\ListInterface@anonymous:private] =\u003e 0\n)\n```\nYou also create a list from an existing array\n```php\n$array = [\"d\", \"e\", \"f\"];\n$otherList = listFromArray($array);\n```\nOutput\n```\nSeboettg\\Collection\\Lists\\ListInterface@anonymous Object\n(\n    [array:Seboettg\\Collection\\Lists\\ListInterface@anonymous:private] =\u003e Array\n        (\n            [0] =\u003e d\n            [1] =\u003e e\n            [2] =\u003e f\n        )\n\n    [offset:Seboettg\\Collection\\Lists\\ListInterface@anonymous:private] =\u003e 0\n)\n```\nAs you may notice, this will reset the array keys\n\nYou can also create an empty List:\n```php\nuse function Seboettg\\Collection\\Lists\\emptyList;\n$emptyList = emptyList();\necho $emptyList-\u003ecount();\n```\noutput\n```\n0\n```\n\n\u003ca name=\"lists-iterate\"/\u003e\n\n### Iterate over lists\n\n```php\nforeach ($list as $key =\u003e $value) {\n    echo \"[\".$key.\"] =\u003e \".$value.\"\\n\";\n}\n```\nOutput:\n```\n[0] =\u003e a\n[1] =\u003e b\n[2] =\u003e c\n```\nor\n```php\nfor ($i = 0; $i \u003c $otherList-\u003ecount(); ++$i) {\n    echo $otherList-\u003eget($i) . \" \";\n}\n```\noutput\n```\nd e f\n```\n\n\u003ca name=\"lists-operations\"/\u003e\n\n### List operations\n\nYou may add the elements of another list to a list by using `plus`:\n\n```php\n$newList = $list-\u003eplus($otherList);\nprint_r($newList);\n```\noutput\n```\nSeboettg\\Collection\\Lists\\ListInterface@anonymous Object\n(\n    [array:Seboettg\\Collection\\Lists\\ListInterface@anonymous:private] =\u003e Array\n        (\n            [0] =\u003e a\n            [1] =\u003e b\n            [2] =\u003e c\n            [3] =\u003e d\n            [4] =\u003e e\n            [5] =\u003e f\n        )\n    [offset:Seboettg\\Collection\\Lists\\ListInterface@anonymous:private] =\u003e 0\n)\n```\nThe same operation is applicable with arrays, with the same result:\n```php\n$newList = $list-\u003eplus($array);\n```\nYou can also subtract the elements of another list or any `iterable` using `minus`:\n```php\n$subtract = $newList-\u003eminus($list);\nprint_r($subtract);\n```\noutput\n```\nSeboettg\\Collection\\Lists\\ListInterface@anonymous Object\n(\n    [array:Seboettg\\Collection\\Lists\\ListInterface@anonymous:private] =\u003e Array\n        (\n            [0] =\u003e d\n            [1] =\u003e e\n            [2] =\u003e f\n        )\n\n    [offset:Seboettg\\Collection\\Lists\\ListInterface@anonymous:private] =\u003e 0\n)\n```\nTo get the intersection of two lists (or an iterable), you can use the `intersect` method:\n```php\n$intersection = $newList-\u003eintersect(listOf(\"b\", \"d\", \"f\", \"h\", \"i\"));\nprint_r($intersection);\n```\noutput\n```\nSeboettg\\Collection\\Lists\\ListInterface@anonymous Object\n(\n    [array:Seboettg\\Collection\\Lists\\ListInterface@anonymous:private] =\u003e Array\n        (\n            [0] =\u003e b\n            [1] =\u003e d\n            [2] =\u003e f\n        )\n\n    [offset:Seboettg\\Collection\\Lists\\ListInterface@anonymous:private] =\u003e 0\n)\n```\nTo get a list containing distinct elements, use `distinct`:\n```php\n$list = listOf(\"a\", \"b\", \"a\", \"d\", \"e\", \"e\", \"g\")\nprint_r($list-\u003edistinct());\n```\noutput\n```\nSeboettg\\Collection\\Lists\\ListInterface@anonymous Object\n(\n    [array:Seboettg\\Collection\\Lists\\ListInterface@anonymous:private] =\u003e Array\n        (\n            [0] =\u003e a\n            [1] =\u003e b\n            [2] =\u003e d\n            [3] =\u003e e\n            [4] =\u003e g\n        )\n\n    [offset:Seboettg\\Collection\\Lists\\ListInterface@anonymous:private] =\u003e 0\n)\n```\n\n\u003ca name=\"lists-map\"/\u003e\n\n### Map all elements of a list\nIf you need to modify all elements in a list, you can do it easily by using the `map` method:\n```php\n$list = listOf(1, 2, 3, 4, 5);\n$cubicList = $list-\u003emap(fn ($i) =\u003e $i * $i * $i);\n//result of $cubicList: 1, 8, 27, 64, 125\n```\n\nThere is also a `mapNotNull` method that eliminates `null` values from the result:\n\n```php\nfunction divisibleByTwoOrNull(int $number): ?int {\n    return $item % 2 === 0 ? $item : null;\n}\n\nlistOf(0, 1, 2, 3, 4, 5)\n    -\u003emap(fn (int $number): ?int =\u003e divisibleByTwoOrNull($number));\n//result: 0, 2, 4\n\n```\n\n\u003ca name=\"lists-filter\"/\u003e\n\n### Filter elements in a list\n\nThe `filter` method returns a list containing only elements matching the given predicate.\n\n```php\n$list = listOf(\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\"):\n$listOfCharactersThatAsciiNumbersIsOdd = $list\n    -\u003efilter(fn($letter) =\u003e ord($letter) % 2 !== 0);\n//result of $listOfCharactersTharOrderNumbersAreOdd: \"a\", \"c\", \"e\", \"g\", \"i\"\n```\n\n\u003ca name=\"lists-logical-operations\"/\u003e\n\n### Logical operations\n\nWith the methods `any` and `all` you can check whether all elements (all) or at least one element (any) match a predicate.\n\n```php\n$list = listOf(\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\"):\n$list-\u003eall(fn($letter) =\u003e ord($letter) % 2 !== 0); // false\n$list-\u003eany(fn($letter) =\u003e ord($letter) % 2 !== 0); // true\n\n$list-\u003eall(fn($letter) =\u003e ord($letter) % 1 !== 0); // true\n$list-\u003eany(fn($letter) =\u003e $letter === \"z\"); // false, since no character in the list is a 'z'\n```\n\n\u003ca name=\"lists-foreach\"/\u003e\n\n### forEach method\n\nWith the forEach method you can apply a closure or lambda functions on each element.\n\n```php\n$list = listOf(\"a\", \"b\", \"c\");\n$list-\u003eforEach(fn (string $item) =\u003e print($item . PHP_EOL));\n```\noutput:\n```\na\nb\nc\n```\n\n\u003ca name=\"lists-sorting\"/\u003e\n\n### Sorting a list\nImplement the Comparable interface \n```php\n\u003c?php\nnamespace Vendor\\App\\Model;\nuse Seboettg\\Collection\\Comparable\\Comparable;\nclass Element implements Comparable\n{\n    private $attribute1;\n    private $attribute2;\n    \n    //contructor\n    public function __construct($attribute1, $attribute2)\n    {\n        $this-\u003eattribute1 = $attribute1;\n        $this-\u003eattribute2 = $attribute2;\n    }\n    \n    // getter\n    public function getAttribute1() { return $this-\u003eattribute1; }\n    public function getAttribute2() { return $this-\u003eattribute2; }\n    \n    //compareTo function\n    public function compareTo(Comparable $b): int\n    {\n        return strcmp($this-\u003eattribute1, $b-\u003egetAttribute1());\n    }\n}\n```\n\nCreate a comparator class \n\n```php\n\u003c?php\nnamespace Vendor\\App\\Util;\n\nuse Seboettg\\Collection\\Comparable\\Comparator;\nuse Seboettg\\Collection\\Comparable\\Comparable;\n\nclass Attribute1Comparator extends Comparator\n{\n    public function compare(Comparable $a, Comparable $b): int\n    {\n        if ($this-\u003esortingOrder === Comparator::ORDER_ASC) {\n            return $a-\u003ecompareTo($b);\n        }\n        return $b-\u003ecompareTo($a);\n    }\n}\n``` \n\nSort your list\n\n```php\n\u003c?php\nuse Seboettg\\Collection\\Lists;\nuse Seboettg\\Collection\\Collections;\nuse Seboettg\\Collection\\Comparable\\Comparator;\nuse function Seboettg\\Collection\\Lists\\listOf;\nuse Vendor\\App\\Util\\Attribute1Comparator;\nuse Vendor\\App\\Model\\Element;\n\n\n$list = listOf(\n    new Element(\"b\",\"bar\"),\n    new Element(\"a\",\"foo\"),\n    new Element(\"c\",\"foobar\")\n);\n\nCollections::sort($list, new Attribute1Comparator(Comparator::ORDER_ASC));\n\n```\n\n#### sort your list using a custom order ####\n\n```php\n\u003c?php\nuse Seboettg\\Collection\\Comparable\\Comparator;\nuse Seboettg\\Collection\\Comparable\\Comparable;\nuse Seboettg\\Collection\\Lists;\nuse Seboettg\\Collection\\Collections;\nuse function Seboettg\\Collection\\Lists\\listOf;\nuse Vendor\\App\\Model\\Element;\n\n//Define a custom Comparator\nclass MyCustomOrderComparator extends Comparator\n{\n    public function compare(Comparable $a, Comparable $b): int\n    {\n        return (array_search($a-\u003egetAttribute1(), $this-\u003ecustomOrder) \u003e= array_search($b-\u003egetAttribute1(), $this-\u003ecustomOrder)) ? 1 : -1;\n    }\n}\n\n$list = listOf(\n    new Element(\"a\", \"aa\"),\n    new Element(\"b\", \"bb\"),\n    new Element(\"c\", \"cc\"),\n    new Element(\"k\", \"kk\"),\n    new Element(\"d\", \"dd\"),\n);\n\nCollections::sort(\n    $list, new MyCustomOrderComparator(Comparator::ORDER_CUSTOM, [\"d\", \"k\", \"a\", \"b\", \"c\"])\n);\n\n```\n\n\u003ca name=\"maps\"/\u003e\n\n## Maps\n\nA Map stores key-value pairs; keys are unique, but different keys can be paired with equal values. The Map interface \nprovides specific methods, such as access to value by key, searching keys and values, and so on.\n\n\u003ca name=\"maps-getting-started\"/\u003e\n\n### Getting started\n\nA Map is a collection of keys that are paired with values. Therefore, to create a Map you need pairs first:\n\n```php\nuse Seboettg\\Collection\\Map\\Pair;\nuse function Seboettg\\Collection\\Map\\pair;\nuse function Seboettg\\Collection\\Map\\mapOf;\n\n$pair1 = pair(\"Ceres\", \"Giuseppe Piazzi\")\n\n//or you use the factory, with the same result:\n$pair2 = Pair::factory(\"Pallas\", \"Heinrich Wilhelm Olbers\");\n\n//Now you can add both pairs to a map\n$map = mapOf($pair1, $pair2);\nprint_r($map);\n```\noutput\n```\nSeboettg\\Collection\\Map\\MapInterface@anonymous Object\n(\n    [array:Seboettg\\Collection\\Map\\MapInterface@anonymous:private] =\u003e Array\n        (\n            [Ceres] =\u003e Giuseppe Piazzi\n            [Pallas] =\u003e Heinrich Wilhelm Olbers\n        )\n\n)\n```\nYou can also create an empty Map:\n```php\nuse function Seboettg\\Collection\\Map\\emptyMap;\n$emptyMap = emptyMap();\necho $emptyMap-\u003ecount();\n```\noutput\n```\n0\n```\n\n### Access elements\n\n```php\nuse function Seboettg\\Collection\\Map\\mapOf;\n$asteroidExplorerMap = mapOf(\n    pair(\"Ceres\", \"Giuseppe Piazzi\"),\n    pair(\"Pallas\", \"Heinrich Wilhelm Olbers\"),\n    pair(\"Juno\", \"Karl Ludwig Harding\"),\n    pair(\"Vesta\", \"Heinrich Wilhelm Olbers\")\n);\n\n$juno = $asteroidExplorerMap-\u003eget(\"Juno\"); //Karl Ludwig Harding\n\n// or access elements like an array\n$pallas = $asteroidExplorerMap[\"Pallas\"]; //Heinrich Wilhelm Olbers\n\n//get a list of all keys\n$asteroids = $asteroidExplorerMap-\u003egetKeys(); //Ceres, Pallas, Juno, Vesta\n\n//get a list of all values\n$explorer = $asteroidExplorerMap\n    -\u003evalues()\n    -\u003edistinct(); // \"Giuseppe Piazzi\", \"Heinrich Wilhelm Olbers\", \"Karl Ludwig Harding\"\n\n$explorer = $asteroidExplorerMap\n    -\u003egetOrElse(\"Iris\", fn() =\u003e \"unknown\"); //$explorer = \"unknown\"\n\n```\nyou are also able to get all map entries as a list of pairs\n```php\n$keyValuePairs = $asteroidExplorerMap-\u003egetEntries();\n```\noutput\n```\nSeboettg\\Collection\\Lists\\ListInterface@anonymous Object\n(\n    [array:Seboettg\\Collection\\Lists\\ListInterface@anonymous:private] =\u003e Array\n        (\n            [0] =\u003e Seboettg\\Collection\\Map\\Pair Object\n                (\n                    [key:Seboettg\\Collection\\Map\\Pair:private] =\u003e Ceres\n                    [value:Seboettg\\Collection\\Map\\Pair:private] =\u003e Giuseppe Piazzi\n                )\n\n            [1] =\u003e Seboettg\\Collection\\Map\\Pair Object\n                (\n                    [key:Seboettg\\Collection\\Map\\Pair:private] =\u003e Pallas\n                    [value:Seboettg\\Collection\\Map\\Pair:private] =\u003e Heinrich Wilhelm Olbers\n                )\n\n            [2] =\u003e Seboettg\\Collection\\Map\\Pair Object\n                (\n                    [key:Seboettg\\Collection\\Map\\Pair:private] =\u003e Juno\n                    [value:Seboettg\\Collection\\Map\\Pair:private] =\u003e Karl Ludwig Harding\n                )\n\n            [3] =\u003e Seboettg\\Collection\\Map\\Pair Object\n                (\n                    [key:Seboettg\\Collection\\Map\\Pair:private] =\u003e Vesta\n                    [value:Seboettg\\Collection\\Map\\Pair:private] =\u003e Heinrich Wilhelm Olbers\n                )\n\n        )\n\n    [offset:Seboettg\\Collection\\Lists\\ListInterface@anonymous:private] =\u003e 0\n)\n```\n\n\u003ca name=\"maps-manipulate\"/\u003e\n\n### Manipulate a map\n\n```php\nuse function Seboettg\\Collection\\Map\\emptyMap;\n\n$map = emptyMap();\n\n//put\n$map-\u003eput(\"ABC\", 1);\necho $map[\"ABC\"]; // 1\n\n//put via array assignment\n$map[\"ABC\"] = 2;\necho $map[\"ABC\"]; // 2\n\n//remove\n$map-\u003eput(\"DEF\", 3);\n$map-\u003eremove(\"DEF\");\necho $map-\u003eget(\"DEF\"); // null\n```\n\n\u003ca name=\"maps-map\"/\u003e\n\n### Map elements\n\nThe signature of given transform function for mapping must have either a `Pair` parameter or a `key` and a `value` parameter.\nThe map function always returns a list of type `ListInterface`:\n\n```php\nuse function Seboettg\\Collection\\Map\\mapOf;\n\nclass Asteroid {\n    public string $name;\n    public ?string $explorer;\n    public ?float $diameter;\n    public function __construct(string $name, string $explorer, float $diameter = null)\n    {\n        $this-\u003ename = $name;\n        $this-\u003eexplorer = $explorer;\n        $this-\u003ediameter = $diameter;\n    }\n}\n\n$asteroids = $asteroidExplorerMap\n    -\u003emap(fn (Pair $pair): Asteroid =\u003e new Asteroid($pair-\u003egetKey(), $pair-\u003egetValue()));\n\nprint_r($asteroids);\n```\noutput\n```\nSeboettg\\Collection\\Lists\\ListInterface@anonymous Object\n(\n    [array:Seboettg\\Collection\\Lists\\ListInterface@anonymous:private] =\u003e Array\n        (\n            [0] =\u003e Asteroid Object\n                (\n                    [name] =\u003e Ceres\n                    [explorer] =\u003e Giuseppe Piazzi\n                    [diameter] =\u003e \n                )\n\n            [1] =\u003e Asteroid Object\n                (\n                    [name] =\u003e Pallas\n                    [explorer] =\u003e Heinrich Wilhelm Olbers\n                    [diameter] =\u003e \n                )\n\n            [2] =\u003e Asteroid Object\n                (\n                    [name] =\u003e Juno\n                    [explorer] =\u003e Karl Ludwig Harding\n                    [diameter] =\u003e \n                )\n\n            [3] =\u003e Asteroid Object\n                (\n                    [name] =\u003e Vesta\n                    [explorer] =\u003e Heinrich Wilhelm Olbers\n                    [diameter] =\u003e \n                )\n\n        )\n\n    [offset:Seboettg\\Collection\\Lists\\ListInterface@anonymous:private] =\u003e 0\n)\n```\nYou get the same result with a key-value signature:\n```php\n$asteroids = $asteroidExplorerMap\n    -\u003emap(fn (string $key, string $value): Asteroid =\u003e new Asteroid($key, $value));\n```\n\n\u003ca name=\"maps-filter\"/\u003e\n\n### Filter entries of a map\n\nYou may filter for elements by this way:\n\n```php\n$asteroidExplorerMap-\u003efilter(fn (Pair $pair): bool =\u003e $pair-\u003egetKey() !== \"Juno\");\n```\nor by this way:\n```php\n$asteroidExplorerMap-\u003efilter(fn (string $key, string $value): bool =\u003e $key !== \"Juno\");\n```\n\n\u003ca name=\"lists-maps\"/\u003e\n\n## Combining Lists and Maps\n\nThere are a lot of opportunities to use lists and maps in real world scenarios with a lot of \nadvantages e.g. less boilerplate code and better code readability. \n\nThe following json file represents a customer file that we want to use for processing.\n\n_customer.json_\n```json\n[\n   {\n       \"id\": \"A001\",\n       \"lastname\": \"Doe\",\n       \"firstname\": \"John\",\n       \"createDate\": \"2022-06-10 09:21:12\"\n   },\n   {\n       \"id\": \"A002\",\n       \"lastname\": \"Doe\",\n       \"firstname\": \"Jane\",\n       \"createDate\": \"2022-06-10 09:21:13\"\n   },\n   {\n       \"id\": \"A004\",\n       \"lastname\": \"Mustermann\",\n       \"firstname\": \"Erika\",\n       \"createDate\": \"2022-06-11 08:21:13\"\n   }\n]\n```\nWe would like to get a map that associates the customer id with respective objects of type `Customer` and we want to apply a filter\nso that we get only customers with lastname Doe.\n\n```php\nuse function Seboettg\\Collection\\Lists\\listFromArray;\n\nclass Customer {\n    public string $id;\n    public string $lastname;\n    public string $firstname;\n    public DateTime $createDate;\n    public function __construct(\n        string $id,\n        string $lastname,\n        string $firstname,\n        DateTime $createDate\n    ) {\n        $this-\u003eid = $id;\n        $this-\u003elastname = $lastname;\n        $this-\u003efirstname = $firstname;\n        $this-\u003ecreateDate = $createDate;\n    }\n}\n\n$customerList = listFromArray(json_decode(file_get_contents(\"customer.json\"), true));\n$customerMap = $customerList\n    -\u003efilter(fn (array $customerArray) =\u003e $customerArray[\"lastname\"] === \"Doe\") // filter for lastname Doe\n    -\u003emap(fn (array $customerArray) =\u003e new Customer(\n        $customerArray[\"id\"],\n        $customerArray[\"lastname\"],\n        $customerArray[\"firstname\"],\n        DateTime::createFromFormat(\"Y-m-d H:i:s\", $customerArray[\"createDate\"])\n     )) // map array to customer object\n    -\u003eassociateBy(fn(Customer $customer) =\u003e $customer-\u003eid); // link the id with the respective customer object\nprint_($customerMap);\n```\noutput\n```\nSeboettg\\Collection\\Map\\MapInterface@anonymous Object\n(\n    [array:Seboettg\\Collection\\Map\\MapInterface@anonymous:private] =\u003e Array\n        (\n            [A001] =\u003e Customer Object\n                (\n                    [id] =\u003e A001\n                    [lastname] =\u003e Doe\n                    [firstname] =\u003e John\n                    [createDate] =\u003e DateTime Object\n                        (\n                            [date] =\u003e 2022-06-10 09:21:12.000000\n                            [timezone_type] =\u003e 3\n                            [timezone] =\u003e UTC\n                        )\n\n                )\n            [A002] =\u003e Customer Object\n                (\n                    [id] =\u003e A002\n                    [lastname] =\u003e Doe\n                    [firstname] =\u003e Jane\n                    [createDate] =\u003e DateTime Object\n                        (\n                            [date] =\u003e 2022-06-10 09:21:13.000000\n                            [timezone_type] =\u003e 3\n                            [timezone] =\u003e UTC\n                        )\n                )\n        )\n)\n```\n\nAnother example: Assuming we have a customer service with a `getCustomerById` method.\nWe have a list of IDs with which we want to request the service. \n\n```php\n$listOfIds = listOf(\"A001\", \"A002\", \"A004\");\n$customerMap = $listOfIds\n    -\u003eassociateWith(fn ($customerId) =\u003e $customerService-\u003egetById($customerId))\n```\noutput\n```\nSeboettg\\Collection\\Map\\MapInterface@anonymous Object\n(\n    [array:Seboettg\\Collection\\Map\\MapInterface@anonymous:private] =\u003e Array\n        (\n            [A001] =\u003e Customer Object\n                (\n                    [id] =\u003e A001\n                    [lastname] =\u003e Doe\n                    [firstname] =\u003e John\n                    [createDate] =\u003e DateTime Object\n                        (\n                            [date] =\u003e 2022-06-10 09:21:12.000000\n                            [timezone_type] =\u003e 3\n                            [timezone] =\u003e UTC\n                        )\n\n                )\n            [A002] ...\n            [A004] ...\n        )\n)\n```\n\u003ca name=\"stack\"/\u003e\n\n## Stack ##\n\nA stack is a collection of elements, with two principal operations:\n\n* push, which adds an element to the collection, and\n* pop, which removes the most recently added element that was not yet removed.\n\nAn Stack is a LIFO data structure: last in, first out. \n\n### Examples ###\n\n#### push, pop and peek ####\n```php\n$stack = new Stack();\n$stack-\u003epush(\"a\")\n      -\u003epush(\"b\")\n      -\u003epush(\"c\");\necho $stack-\u003epop(); // outputs c\necho $stack-\u003ecount(); // outputs 2\n\n// peek returns the element at the top of this stack without removing it from the stack.\necho $stack-\u003epeek(); // outputs b\necho $stack-\u003ecount(); // outputs 2\n```\n\n#### search ####\nThe search function returns the position where an element is on this stack. If the passed element occurs as an element \nin this stack, this method returns the distance from the top of the stack of the occurrence nearest the top of the \nstack; the topmost element on the stack is considered to be at distance 1. If the passed element does not occur in \nthe stack, this method returns 0.\n\n```php\necho $stack-\u003esearch(\"c\"); //outputs 0 since c does not exist anymore\necho $stack-\u003esearch(\"a\"); //outputs 2\necho $stack-\u003esearch(\"b\"); //outputs 1\n```\n\n\u003ca name=\"queue\"/\u003e\n\n## Queue ##\nA queue is a collection in which the elements are kept in order. A queue has two principle operations:\n\n* enqueue\n* dequeue \n\n### Examples ###\n```php\n$queue = new Queue();\n$queue-\u003eenqueue(\"d\")\n    -\u003eenqueue(\"z\")\n    -\u003eenqueue(\"b\")\n    -\u003eenqueue(\"a\");\n    \necho $queue-\u003edequeue(); // outputs d\necho $queue-\u003edequeue(); // outputs z\necho $queue-\u003edequeue(); // outputs b\necho $queue-\u003ecount(); // outputs 1\n```\n\n\u003ca name=\"contribution\"/\u003e\n\n## Contribution ##\nFork this Repo and feel free to contribute your ideas using pull requests.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseboettg%2Fcollection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fseboettg%2Fcollection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseboettg%2Fcollection/lists"}