{"id":22764586,"url":"https://github.com/kuria/collections","last_synced_at":"2025-06-28T12:32:02.932Z","repository":{"id":57009929,"uuid":"132900522","full_name":"kuria/collections","owner":"kuria","description":"Object-oriented collection structures","archived":false,"fork":false,"pushed_at":"2023-04-22T14:38:39.000Z","size":41,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-30T10:14:10.709Z","etag":null,"topics":["collection","map","php"],"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/kuria.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.rst","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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-05-10T12:57:12.000Z","updated_at":"2023-09-19T17:45:40.000Z","dependencies_parsed_at":"2025-02-05T12:12:05.492Z","dependency_job_id":"9d0bc501-0199-4d60-a377-c0ec1b80f837","html_url":"https://github.com/kuria/collections","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/kuria/collections","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuria%2Fcollections","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuria%2Fcollections/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuria%2Fcollections/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuria%2Fcollections/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kuria","download_url":"https://codeload.github.com/kuria/collections/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kuria%2Fcollections/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262431283,"owners_count":23310005,"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","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":["collection","map","php"],"created_at":"2024-12-11T12:09:24.223Z","updated_at":"2025-06-28T12:32:02.909Z","avatar_url":"https://github.com/kuria.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Collections\n###########\n\nObject-oriented collection structures.\n\n.. image:: https://travis-ci.com/kuria/collections.svg?branch=master\n   :target: https://travis-ci.com/kuria/collections\n\n.. contents::\n\n\nFeatures\n********\n\n- ``Collection`` - list of values with sequential integer indexes\n- ``Map`` - key-value map\n\n\nRequirements\n************\n\n- PHP 7.1+\n\n\nCollection\n**********\n\nThe ``Collection`` class implements a list of values with sequential integer indexes.\n\nIt also implements ``Countable``, ``ArrayAccess`` and ``IteratorAggregate``.\n\n\nCreating a new collection\n=========================\n\nEmpty collection\n----------------\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\Collections\\Collection;\n\n   $collection = Collection::create();\n\n\nUsing an existing `iterable \u003chttp://php.net/manual/en/language.types.iterable.php\u003e`_\n------------------------------------------------------------------------------------\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\Collections\\Collection;\n\n   $collection = Collection::create(['foo', 'bar', 'baz']);\n\n\nUsing varargs\n-------------\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\Collections\\Collection;\n\n   $collection = Collection::collect('foo', 'bar', 'baz');\n\n\nCollection method overview\n==========================\n\nRefer to doc comments of the respective methods for more information.\n\n\nStatic methods\n--------------\n\n- ``create($values = null): self`` - create a collection from an iterable\n- ``collect(...$values): self`` - create a collection from the passed arguments\n- ``fill($value, $count): self`` - create a collection and populate it with repeated values\n- ``explode($string, $delimiter, $limit = PHP_INT_MAX): self`` - create a collection by splitting a string\n\n\nInstance methods\n----------------\n\n- ``setValues($values): void`` - replace all values with the given iterable\n- ``toArray(): array`` - get all values as an array\n- ``isEmpty(): bool`` - see if the collection is empty\n- ``count(): int`` - count values\n- ``has($index): bool`` - see if the given index exists\n- ``contains($value, $strict = true): bool`` - see if the given value exists\n- ``find($value, $strict = true): ?int`` - try to find the first occurence of a value\n- ``get($index): mixed`` - get value at the given index\n- ``first(): mixed`` - get first value\n- ``last(): mixed`` - get last value\n- ``indexes(): int[]`` - get all indexes\n- ``slice($index, $length = null): self`` - extract a slice of the collection\n- ``replace($index, $value): void`` - replace a value at the given index\n- ``push(...$values): void`` - push one or more values onto the end of the collection\n- ``pop(): mixed`` - pop a value off the end of the collection\n- ``unshift(...$values): void`` - prepend one or more values to the beginning of the collection\n- ``shift(): mixed`` - shift a value off the beginning of the collection\n- ``insert($index, ...$values): void`` - insert one or more values at the given index\n- ``pad($length, $value): void`` - pad the collection with a value to the specified length\n- ``remove(...$indexes): void`` - remove values at the given indexes\n- ``clear(): void`` - remove all values\n- ``splice($index, $length = null, $replacement = null): void`` - remove or replace a part of the collection\n- ``sum(): int|float`` - calculate the sum of all values\n- ``product(): int|float`` - calculate the product of all values\n- ``implode($delimiter = ''): string`` - join all values using a delimiter\n- ``reduce($callback, $initial = null): mixed`` - reduce the collection to a single value\n- ``reverse(): self`` - reverse the collection\n- ``chunk($size): self[]`` - split the collection into chunks of the given size\n- ``split($number): self[]`` - split the collection into the given number of chunks\n- ``unique(): self`` - get unique values\n- ``shuffle(): self`` - get values in random order\n- ``random($count): self`` - get N random values from the collection\n- ``column($key): self`` - gather values from a property or array index of all object or array values\n- ``mapColumn($valueKey, $indexKey): Map`` - build a map using properties or array indexes of all object or array values\n- ``filter($filter): self`` - filter values using the given callback\n- ``apply($callback): self`` - apply the callback to all values\n- ``map($mapper): Map`` - convert the collection to a map\n- ``merge(...$iterables): self`` - merge the collection with the given iterables\n- ``intersect(...$iterables): self`` - compute an intersection with the given iterables\n- ``uintersect($comparator, ...$iterables): self`` - compute an intersection with the given iterables using a custom comparator\n- ``diff(...$iterables): self`` - compute a difference between this collection and the given iterables\n- ``udiff($comparator, ...$iterables): self`` - compute a difference between this collection and the given iterables using a custom comparator\n- ``sort($flags = SORT_REGULAR, $reverse = false): self`` - sort the collection\n- ``usort($comparator): self`` - sort the collection using a custom comparator\n\n.. NOTE::\n\n   Any method that returns ``self`` returns a new collection instance with the selected or modified values.\n   The original collection is not changed.\n\n   If updating the original collection is needed, use ``setValues()`` to do so, e.g.:\n\n   .. code:: php\n\n      \u003c?php\n\n      $collection-\u003esetValues($collection-\u003esort());\n\nArray access and iteration\n==========================\n\n``Collection`` instances can be accessed and iterated as regular arrays.\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\Collections\\Collection;\n\n   $collection = Collection::create();\n\n   // push some values\n   $collection[] = 'foo';\n   $collection[] = 'bar';\n   $collection[] = 'baz';\n\n   // replace a value\n   $collection[1] = 'new bar';\n\n   // remove a value\n   unset($collection[2]);\n\n   // read values\n   echo 'Value at index 1 is ', $collection[1], \"\\n\";\n   echo 'Value at index 2 ', isset($collection[2]) ? 'exists' : 'does not exist', \"\\n\";\n\n   // count values\n   echo 'There are ', count($collection), ' values in total', \"\\n\";\n\n   // iterate values\n   foreach ($collection as $index =\u003e $value) {\n       echo $index, ': ', $value, \"\\n\";\n   }\n\nOutput:\n\n::\n\n  Value at index 1 is new bar\n  Value at index 2 does not exist\n  There are 2 values in total\n  0: foo\n  1: new bar\n\n\nMap\n***\n\nThe ``Map`` class implements a key value map.\n\nIt also implements ``Countable``, ``ArrayAccess`` and ``IteratorAggregate``.\n\n\nCreating a new map\n==================\n\nEmpty map\n---------\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\Collections\\Map;\n\n   $map = Map::create();\n\n\nUsing an existing `iterable \u003chttp://php.net/manual/en/language.types.iterable.php\u003e`_\n------------------------------------------------------------------------------------\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\Collections\\Map;\n\n   $map = Map::create(['foo' =\u003e 'bar', 'bar' =\u003e 'baz']);\n\n\nMap method overview\n===================\n\nRefer to doc comments of the respective methods for more information.\n\nStatic methods\n--------------\n\n- ``create($pairs = null): self`` - create a map from an iterable\n- ``map($iterable, $mapper): self`` - map values of the given iterable using a callback\n- ``build($iterable, $mapper): self`` - build a map from an iterable using a callback\n- ``combine($keys, $values): self`` - combine a list of keys and a list of values to create a map\n\n\nInstance methods\n----------------\n\n- ``setPairs($pairs): void`` - replace all pairs with the given iterable\n- ``toArray(): array`` - get all pairs as an array\n- ``isEmpty(): bool`` - see if the map is empty\n- ``count(): int`` - count pairs\n- ``has($key): bool`` - see if the given key exists\n- ``contains($value, $strict = true): bool`` - see if the given value exists\n- ``find($value, $strict = true): string|int|null`` - try to find the first occurence of a value\n- ``get($key): mixed`` - get value for the given key\n- ``values(): Collection`` - get all values\n- ``keys(): Collection`` - get all keys\n- ``set($key, $value): void`` - define a pair\n- ``add(...$iterables): void`` - add pairs from other iterables to this map\n- ``fill($keys, $value): void`` - fill specific keys with a value\n- ``remove(...$keys): void`` - remove pairs with the given keys\n- ``clear(): void`` - remove all pairs\n- ``reduce($reducer, $initial = null): mixed`` - reduce the map to a single value\n- ``flip(): self`` - swap keys and values\n- ``shuffle(): self`` - randomize pair order\n- ``column($key, $indexBy = null): self`` - gather values from properties or array keys of all object or array values\n- ``filter($filter): self`` - filter pairs using the given callback\n- ``apply($callback): self`` - apply the callback to all pairs\n- ``map($mapper): self`` - remap pairs using the given callback\n- ``intersect(...$iterables): self`` - compute an intersection with the given iterables\n- ``uintersect($comparator, ...$iterables): self`` - compute an intersection with the given iterables using a custom comparator\n- ``intersectKeys(...$iterables): self`` - compute a key intersection with the given iterables\n- ``uintersectKeys($comparator, ...$iterables): self`` - compute a key intersection with the given iterables using a custom comparator\n- ``diff(...$iterables): self`` - compute a difference between this map and the given iterables\n- ``udiff($comparator, ...$iterables): self`` - compute a difference between this map and the given iterables using a custom comparator\n- ``diffKeys(...$iterables): self`` - compute a key difference between this map and the given iterables\n- ``udiffKeys($comparator, ...$iterables): self`` - compute a key difference between this map and the given iterables using a custom comparator\n- ``sort($flags = SORT_REGULAR, $reverse = false): self`` - sort the map using its values\n- ``usort($comparator): self`` - sort the map using its values and a custom comparator\n- ``ksort($flags = SORT_REGULAR, $reverse = false): self`` - sort the map using its keys\n- ``uksort(): self`` - sort the map using its keys and a custom comparator\n\n.. NOTE::\n\n   Any method that returns ``self`` returns a new map instance with the selected or modified pairs.\n   The original map is not changed.\n\n   If updating the original map is needed, use ``setPairs()`` to do so, e.g.:\n\n   .. code:: php\n\n      \u003c?php\n\n      $map-\u003esetPairs($map-\u003esort());\n\n\nArray access and iteration\n==========================\n\n``Map`` instances can be accessed and iterated as regular arrays.\n\n.. code:: php\n\n   \u003c?php\n\n   use Kuria\\Collections\\Map;\n\n   $map = Map::create();\n\n   // add some pairs\n   $map['foo'] = 'bar';\n   $map['baz'] = 'qux';\n   $map['quux'] = 'quuz';\n\n   // remove a pair\n   unset($map['baz']);\n\n   // read values\n   echo 'Value with key \"foo\" is ', $map['foo'], \"\\n\";\n   echo 'Value with key \"baz\" ', isset($map['baz']) ? 'exists' : 'does not exist', \"\\n\";\n\n   // count pairs\n   echo 'There are ', count($map), ' pairs in total', \"\\n\";\n\n   // iterate pairs\n   foreach ($map as $key =\u003e $value) {\n      echo $key, ': ', $value, \"\\n\";\n   }\n\nOutput:\n\n::\n\n  Value with key \"foo\" is bar\n  Value with key \"baz\" does not exist\n  There are 2 pairs in total\n  foo: bar\n  quux: quuz\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkuria%2Fcollections","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkuria%2Fcollections","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkuria%2Fcollections/lists"}