{"id":36334581,"url":"https://github.com/mleczek/collections","last_synced_at":"2026-01-11T12:04:17.389Z","repository":{"id":57018162,"uuid":"363417241","full_name":"mleczek/collections","owner":"mleczek","description":"Manipulate arrays in PHP","archived":false,"fork":false,"pushed_at":"2021-07-26T20:25:20.000Z","size":29,"stargazers_count":13,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-24T17:46:25.675Z","etag":null,"topics":["array","collection","manipulation"],"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/mleczek.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":"contributing.md","funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-05-01T13:31:18.000Z","updated_at":"2021-07-26T20:25:24.000Z","dependencies_parsed_at":"2022-08-22T11:31:43.371Z","dependency_job_id":null,"html_url":"https://github.com/mleczek/collections","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/mleczek/collections","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mleczek%2Fcollections","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mleczek%2Fcollections/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mleczek%2Fcollections/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mleczek%2Fcollections/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mleczek","download_url":"https://codeload.github.com/mleczek/collections/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mleczek%2Fcollections/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28302194,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-11T11:18:18.743Z","status":"ssl_error","status_checked_at":"2026-01-11T11:07:56.842Z","response_time":60,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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","collection","manipulation"],"created_at":"2026-01-11T12:04:17.322Z","updated_at":"2026-01-11T12:04:17.377Z","avatar_url":"https://github.com/mleczek.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Collections\n\nThis package was created to provide simple way to manipulate arrays in PHP. The package was inspired by the Laravel Collections.\n\n## Installation\n\n```\ncomposer require mleczek/collections\n```\n\n## Getting started\n\nConvert any array to collection:\n\n```php\n$collection = new \\Mleczek\\Collections\\Collection([\n    ['id' =\u003e 3, 'firstName' =\u003e 'Debra', 'lastName' =\u003e 'Barnett'],\n    ['id' =\u003e 8, 'firstName' =\u003e 'Ronnie', 'lastName' =\u003e 'Coleman'],\n    ['id' =\u003e 2, 'firstName' =\u003e 'Gabriel', 'lastName' =\u003e 'Adams'],\n]);\n\n// ...and perform some operations:\n$names = $collection\n    -\u003ewhereIn(fn($x) =\u003e $x['id'], [3, 2])\n    -\u003emap(fn($x) =\u003e $x['firstName'] .' '. $x['lastName'])\n    -\u003ejoin(', ');\n\n// $names is equal \"Debra Barnett, Gabriel Adams\"\n```\n\nYou can also do this using `collection` helper method:\n\n```php\n$sum = collection([1, 2, 5])\n    -\u003emap(fn($x) =\u003e $x * 2)\n    -\u003esum();\n\n// $sum is equal 16 (2+4+10)\n```\n\n## Available operations\n\n- [addFirst](#addFirst)\n- [addLast](#addLast)\n- [avg](#avg)\n- [chunk](#chunk)\n- [count](#count)\n- [each](#each)\n- [firstKey](#firstKey)\n- [first](#first)\n- [flatten](#flatten)\n- [groupBy](#groupBy)\n- [has](#has)\n- [isAssociative](#isAssociative)\n- [isEmpty](#isEmpty)\n- [isIndexed](#isIndexed)\n- [isNotEmpty](#isNotEmpty)\n- [join](#join)\n- [keyBy](#each)\n- [keys](#keys)\n- [lastKey](#lastKey)\n- [last](#last)\n- [map](#map)\n- [max](#max)\n- [merge](#merge)\n- [min](#min)\n- [randomKey](#randomKey)\n- [random](#random)\n- [reduce](#reduce)\n- [removeFirst](#removeFirst)\n- [removeLast](#removeLast)\n- [reverse](#reverse)\n- [skip](#skip)\n- [sortDesc](#sortDesc)\n- [sort](#sort)\n- [sum](#sum)\n- [take](#take)\n- [toArray](#toArray)\n- [unique](#unique)\n- [values](#values)\n- [whereIn](#whereIn)\n- [where](#where)\n\n### addFirst\n\nAdd item at the beginning of the collection.\n\n```php\n$collection = collection([2, 3])-\u003eaddFirst(1); // [1, 2, 3]\n```\n\n### addLast\n\nAdd item at the end of the collection.\n\n```php\n$collection = collection([2, 3])-\u003eaddLast(4); // [2, 3, 4]\n```\n\n### avg\n\nCalculate average value from items. Zero if there're no items in collection.\n\n```php\n$avg = collection([1, 2, 6])-\u003eavg(); // 3\n``` \n\n```php\n$items = [\n    ['value' =\u003e 3],\n    ['value' =\u003e 7],\n];\n\n$avg = collection($items)\n    -\u003eavg(fn($item, $key) =\u003e $item['value']); // 5\n```\n\nThrows when trying to calculate avg from non-number value.\n\n### chunk\n\nBreaks into multiple, smaller collections of a given size.\n\nE.g. for chunk size 2 the output array for `[1, 2, 3]` will be `[[1, 2], [3]]`.\n\n```php\n$collection = collection([1, 2, 3])-\u003echunk(2); // [[1, 2], [3]]\n```\n\n### count\n\nThe total number of items in the collection.\n\n```php\n$count = collection([1, 2])-\u003ecount(); // 2\n```\n\n### each\n\nIterate over each item.\n\n```php\ncollection(['a' =\u003e 1, 'b' =\u003e 2])\n    -\u003eeach(fn($item, $key) =\u003e printf(\"$key:$item\"));\n```\n\n### firstKey\n\nGet first item key.\n\n```php\n$key = collection(['a' =\u003e 1, 'b' =\u003e 2])-\u003efirstKey(); // 'a'\n```\n\n### first\n\nGet first item value.\n\n```php\n$item = collection(['a' =\u003e 1, 'b' =\u003e 2])-\u003efirst(); // 1\n```\n\n### flatten\n\nConvert array of arrays to array (remove one dimension).\n\n```php\n$collection = collection([[1, 2], [3]])-\u003eflatten(); // [1, 2, 3]\n```\n\n### groupBy\n\nGroups the collection's items by a given key.\n\n```php\n$items = [\n    ['brand' =\u003e 'Jeep', 'model' =\u003e 'Cherokee Latitude'],\n    ['brand' =\u003e 'Nissan', 'model' =\u003e 'Sentra SV'],\n    ['brand' =\u003e 'Nissan', 'model' =\u003e 'Murano Platinum'],\n];\n\n$collection = collection($items)\n    -\u003egroupBy(fn($item, $key) =\u003e $item['brand']); // ['Jeep' =\u003e [...], 'Nissan' =\u003e [...]]\n```\n\n### has\n\nCheck whether collection has items which match given closure.\n\n```php\n$test = collection([2, 7, 3])\n    -\u003ehas(fn($item, $key) =\u003e $item === 7); // true\n```\n\n### isAssociative\n\nCheck whether collection is an associative array.\n\n```php\n$test = collection([1, 4])-\u003eisAssociative(); // false\n$test = collection(['a' =\u003e 1, 4])-\u003eisAssociative(); // true\n```\n\nSee also [`isIndexed`](#isIndexed) method.\n\n### isEmpty\n\nCheck whether collection has zero items.\n\n```php\n$test = collection([])-\u003eisEmpty(); // true\n```\n\n### isIndexed\n\nCheck whether collection is an indexed array.\n\n```php\n$test = collection([1, 4])-\u003eisIndexed(); // true\n$test = collection(['a' =\u003e 1, 4])-\u003eisIndexed(); // false\n```\n\nSee also [`isAssociative`](#isAssociative) method.\n\n### isNotEmpty\n\nCheck whether collection has any item.\n\n```php\n$test = collection([8, 2])-\u003eisNotEmpty(); // true\n```\n\n### join\n\nJoin all items with given glue.\n\n```php\n$string = collection(['Nissan', 'Jeep', 'Ford'])-\u003ejoin(', '); // 'Nissan, Jeep, Ford'\n```\n\n### keyBy\n\nChange collection's items keys.\n\n```php\n$items = [\n    ['id' =\u003e 5, 'username' =\u003e 'lorraine'],\n    ['id' =\u003e 1, 'username' =\u003e 'gabriel.hill'],\n    ['id' =\u003e 4, 'username' =\u003e 'steward'],\n];\n\n$collection = collection($items)\n    -\u003ekeyBy(fn($item, $key) =\u003e $item['id']); // [5 =\u003e [...], 1 =\u003e [...], 4 =\u003e [...]]\n```\n\nIf multiple items have the same key, the exception will be thrown.\n\n### keys\n\nReturns all of the collection's keys.\n\n```php\n$array = collection(['a' =\u003e 1, 'b' =\u003e 3])-\u003ekeys(); // ['a', 'b']\n```\n\n### lastKey\n\nGet last item key.\n \n ```php\n$key = collection(['a' =\u003e 1, 'b' =\u003e 2])-\u003elastKey(); // 'b'\n ```\n\n### last\n\nGet last item value.\n \n ```php\n$item = collection(['a' =\u003e 1, 'b' =\u003e 2])-\u003elast(); // 2\n ```\n\n### map\n\nIterates through the collection and modify each item.\n\n```php\n$collection = collection([1, 4])\n    -\u003emap(fn($item, $key) =\u003e $item * 2); // [2, 8]\n```\n\nArray keys are preserved.\n\n### max\n\nGet maximum value from items.\n\nThe \"\u003e\" operator is used to find maximum value.\n\n```php\n$max = collection([1, 4, 7])-\u003emax(); // 7\n```\n\n```php\n$items = [\n    ['value' =\u003e 3],\n    ['value' =\u003e 8],\n];\n\n$avg = collection($items)\n    -\u003emax(fn($item, $key) =\u003e $item['value']); // 8\n```\n\n### merge\n\nMerge collection/array to current array.\n\nIn associative arrays values for existing keys will be overwrite.\nIn indexed arrays new values are always appended at the end of collection.\n\n```php\n$collection = collection([1, 2])-\u003emerge([3, 4]); // [1, 2, 3, 4]\n```\n\n```php\n$first = collection([1, 2]);\n$second = collection(['a' =\u003e 1, 'b' =\u003e 2]);\n\n$collection = $first-\u003emerge($second);// [1, 2, 'a' =\u003e 1, 'b' =\u003e 2]\n```\n\n### min\n\nGet minimum value from items.\n\nThe \"\u003c\" operator is used to find minimum value.\n\n```php\n$max = collection([1, 4, 7])-\u003emin(); // 1\n```\n\n```php\n$items = [\n    ['value' =\u003e 3],\n    ['value' =\u003e 8],\n];\n\n$avg = collection($items)\n    -\u003emin(fn($item, $key) =\u003e $item['value']); // 3\n```\n\n### randomKey\n\nGet random key from collection.\n\nReturns null if collection is empty.\n\n```php\n$item = collection(['a' =\u003e 1, 'b' =\u003e 2])-\u003erandomKey();\n```\n\n### random\n\nGet random item value.\n\nReturns null if collection is empty.\n\n```php\n$item = collection([1, 8, 4])-\u003erandom();\n```\n\n### reduce\n\nReduces the collection to a single value, passing the result of each iteration into the subsequent iteration.\n\n```php\n$initialState = 2;\n$result = collection([1, 8, 4])\n    -\u003ereduce(fn($item, $state) =\u003e $state + $item, $initialState); // 15\n```\n\n### removeFirst\n\nRemove first N items from collection.\n\n```php\n$collection = collection([1, 8, 4])-\u003eremoveFirst(); // [8, 4]\n$collection = collection([1, 8, 4])-\u003eremoveFirst(2); // [4]\n```\n\n### removeLast\n\nRemove last N items from collection.\n\n```php\n$collection = collection([1, 8, 4])-\u003eremoveLast(); // [1, 8]\n$collection = collection([1, 8, 4])-\u003eremoveLast(2); // [1]\n```\n\n### reverse\n\nReverse items order.\n\n```php\n$collection = collection([1, 8, 4])-\u003ereverse(); // [4, 8, 1]\n```\n\n### skip\n\nSkip N first items.\n\n```php\n$collection = collection([1, 8, 4])-\u003eskip(); // [8, 4]\n$collection = collection([1, 8, 4])-\u003eskip(2); // [4]\n```\n\n### sortDesc\n\nSort items descending.\n\nStrings are sorted in case insensitive manner.\n\n```php\n$collection = collection([1, 8, 4])-\u003esortDesc(); // [8, 4, 1]\n```\n\n```php\n$items = [\n    ['value' =\u003e 3],\n    ['value' =\u003e 7],\n];\n\n$collection = collection($items)\n    -\u003esortDesc(fn($item) =\u003e $item['value']); // ['value' =\u003e 7, 'value' =\u003e 3]\n```\n\nSee also [`sort`](#sort) method.\n\n### sort\n\nSort items ascending.\n\nStrings are sorted in case insensitive manner.\n\n```php\n$collection = collection([1, 8, 4])-\u003esort(); // [1, 4, 8]\n```\n\n```php\n$items = [\n    ['value' =\u003e 3],\n    ['value' =\u003e 7],\n];\n\n$collection = collection($items)\n    -\u003esort(fn($item) =\u003e $item['value']); // ['value' =\u003e 3, 'value' =\u003e 7]\n```\n\nSee also [`sortDesc`](#sortDesc) method.\n\n### sum\n\nReturns the sum of all items in the collection.\n\n```php\n$sum = collection([1, 2, 6])-\u003esum(); // 9\n``` \n\n```php\n$items = [\n    ['value' =\u003e 3],\n    ['value' =\u003e 7],\n];\n\n$sum = collection($items)\n    -\u003esum(fn($item, $key) =\u003e $item['value']); // 10\n```\n\nThrows when trying to calculate sum from non-number value.\n\n### take\n\nTake N first items.\n\n```php\n$collection = collection([1, 8, 4])-\u003etake(); // [1]\n$collection = collection([1, 8, 4])-\u003etake(2); // [1, 8]\n```\n\n### toArray\n\nReturns collection's items.\n\n```php\n$array = collection([6, 3, 1])-\u003etoArray(); // [6, 3, 1]\n```\n\n### unique\n\nLeft only items with unique value.\n\n```php\n$collection = collection([6, 1, 3, 1])-\u003eunique(); // [6, 1, 3]\n```\n\nFirst occurrence is taken if multiple same values are encountered.\n\n```php\n$items = [\n    ['brand' =\u003e 'Jeep', 'model' =\u003e 'Cherokee Latitude'],\n    ['brand' =\u003e 'Nissan', 'model' =\u003e 'Sentra SV'],\n    ['brand' =\u003e 'Nissan', 'model' =\u003e 'Murano Platinum'],\n];\n\n$collection = collection([$items])\n    -\u003eunique(fn($item, $key) =\u003e $item['brand']);\n```\n\n### values\n\nReturns all of the collection's values (as indexed array).\n\n```php\n$values = collection(['a' =\u003e 1, 'b' =\u003e 2])-\u003evalues(); // [1, 2]\n```\n\n### whereIn\n\nReturn collection with items that needle is in haystack of accepted values.\n\n```php\n$collection = collection([8, 4, 2])\n    -\u003ewhereIn(fn($item, $key) =\u003e $item, [4, 7, 2]); // [4, 2]\n```\n\n### where\n\nReturn collection with items which match given criteria.\n\n```php\n$collection = collection([8, 4, 2])\n    -\u003ewhere(fn($item, $key) =\u003e $item \u003e 3); // [8, 4]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmleczek%2Fcollections","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmleczek%2Fcollections","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmleczek%2Fcollections/lists"}