{"id":15024164,"url":"https://github.com/izica/php-collection","last_synced_at":"2025-04-09T20:02:20.662Z","repository":{"id":56995142,"uuid":"177607203","full_name":"izica/php-collection","owner":"izica","description":"Tools and utilities for working with arrays","archived":false,"fork":false,"pushed_at":"2023-04-09T01:03:01.000Z","size":23,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-09T20:01:59.124Z","etag":null,"topics":["array","arrays","collection","collections","illuminate","library","lodash","php","php-array","php-collection","php-library","php-lodash","php5","php56","php7","tool","tools","utilities","utility","utils"],"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/izica.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-25T14:52:48.000Z","updated_at":"2024-09-20T09:48:23.000Z","dependencies_parsed_at":"2024-10-12T12:20:43.225Z","dependency_job_id":null,"html_url":"https://github.com/izica/php-collection","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izica%2Fphp-collection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izica%2Fphp-collection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izica%2Fphp-collection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izica%2Fphp-collection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/izica","download_url":"https://codeload.github.com/izica/php-collection/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248103864,"owners_count":21048245,"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":["array","arrays","collection","collections","illuminate","library","lodash","php","php-array","php-collection","php-library","php-lodash","php5","php56","php7","tool","tools","utilities","utility","utils"],"created_at":"2024-09-24T19:59:53.361Z","updated_at":"2025-04-09T20:02:20.575Z","avatar_url":"https://github.com/izica.png","language":"PHP","readme":"# Php collection\n\ninspired by Illuminate\\Support\\Collection and Lodash\n\n### Usage\n```\ncomposer require izica/php-collection\n```\n\n### Notice\nEvery method will returns new collection, not mutated collection;\n\n```php\n\n$collection = PhpCollection:collect([100, 200, 300, 400]);\n$collection2 = $collection-\u003efilter(function($item){\n    return $item \u003e 200;\n})\n\n/*\n    $collection != $collection2\n*/\n\n```\n\n### Documentation\n\n* [collect](#collectarray)\n* [implode OR join](#implodearray----serializer---or-joinarray----serializer--)\n* [pluck](#pluckkey)\n* [only](#onlykeys)\n* [exclude](#excludekeys)\n* [filter](#filterfunctionitem)\n* [map](#mapfunctionitem)\n* [keyBy](#keybykey)\n* [groupBy](#groupbykey)\n* [find](#findfunctionitem)\n* [some OR contains](#somefunctionitem-or-containsfunctionitem)\n* [every](#everyfunctionitem)\n* [sort](#sortfunctionitem)\n* [values](#values)\n* [first](#first)\n* [last](#last)\n* [count](#count)\n* [all or toArray](#all-or-toarray)\n* [toJson](#tojson)\n\n\n#### collect($array, $isSingleElement = false)\n```php\n$products = [\n    [\"id\" =\u003e 1, \"name\" =\u003e \"product 1\", \"price\" =\u003e 100],\n    [\"id\" =\u003e 2, \"name\" =\u003e \"product 2\", \"price\" =\u003e 200],\n    [\"id\" =\u003e 3, \"name\" =\u003e \"product 3\", \"price\" =\u003e 300]\n];\n\n$collection = PhpCollection:collect($products);\n\n// make collection from single element\n$collection = PhpCollection:collect($products[0], true);\n\n```\n\n#### implode($array = \", \", $serializer = \"\") OR join($array = \", \", $serializer = \"\")\n\n```php\n$collection = PhpCollection:collect([100, \"data\", 300, 400])-\u003eimplode();\n/*\n    100, data, 300, 400\n*/\n\n$products = [\n    [\"id\" =\u003e 1, \"name\" =\u003e \"product 1\", \"price\" =\u003e 100],\n    [\"id\" =\u003e 2, \"name\" =\u003e \"product 2\", \"price\" =\u003e 200],\n    [\"id\" =\u003e 3, \"name\" =\u003e \"product 3\", \"price\" =\u003e 300]\n];\n\n// should return string\n$serializer = function($item){\n    return \"{$item['name']}-{$item['price']}$\"; // or for example -- return json_encode($item);\n};\n\n$collection = PhpCollection:collect($products)-\u003eimplode(\", \", $serializer);\n/*\n    product 1-100$, product 2-200$, product 3-300$\n*/\n```\n\n#### pluck($key)\n```php\n$products = [\n    [\"id\" =\u003e 1, \"name\" =\u003e \"product 1\", \"price\" =\u003e 100],\n    [\"id\" =\u003e 2, \"name\" =\u003e \"product 2\", \"price\" =\u003e 200],\n    [\"id\" =\u003e 3, \"name\" =\u003e \"product 3\", \"price\" =\u003e 300]\n];\n\n$collection = PhpCollection:collect($products)-\u003epluck(\"id\")-\u003eall();\n/*\n    [1, 2, 3]\n*/  \n\n$collection = PhpCollection:collect($products)-\u003epluck(\"name\")-\u003eall();\n/*\n    [\"product 1\", \"product 2\", \"product 3\"]\n*/ \n```\n\n#### only($keys)\n```php\n$products = [\n    [\"id\" =\u003e 1, \"name\" =\u003e \"product 1\", \"price\" =\u003e 100],\n    [\"id\" =\u003e 2, \"name\" =\u003e \"product 2\", \"price\" =\u003e 200],\n    [\"id\" =\u003e 3, \"name\" =\u003e \"product 3\", \"price\" =\u003e 300]\n];\n\n$collection = PhpCollection:collect($products)-\u003eonly([\"id\", \"name\"])-\u003eall();\n/*\n[\n    [\"id\" =\u003e 1, \"name\" =\u003e \"product 1\"],\n    [\"id\" =\u003e 2, \"name\" =\u003e \"product 2\"],\n    [\"id\" =\u003e 3, \"name\" =\u003e \"product 3\"]\n]\n*/\n\n$collection = PhpCollection:collect($products)-\u003eonly([\"id\", \"name\" =\u003e \"title\"])-\u003eall();\n\n/*\n[\n    [\"id\" =\u003e 1, \"title\" =\u003e \"product 1\"],\n    [\"id\" =\u003e 2, \"title\" =\u003e \"product 2\"],\n    [\"id\" =\u003e 3, \"title\" =\u003e \"product 3\"]\n]\n*/\n\n```\n\n#### exclude($keys)\n```php\n$products = [\n    [\"id\" =\u003e 1, \"name\" =\u003e \"product 1\", \"price\" =\u003e 100],\n    [\"id\" =\u003e 2, \"name\" =\u003e \"product 2\", \"price\" =\u003e 200],\n    [\"id\" =\u003e 3, \"name\" =\u003e \"product 3\", \"price\" =\u003e 300]\n];\n\n$collection = PhpCollection:collect($products)-\u003eexclude([\"name\"])-\u003eall();\n\n/*\n[\n    [\"id\" =\u003e 1, \"price\" =\u003e 100],\n    [\"id\" =\u003e 2, \"price\" =\u003e 200],\n    [\"id\" =\u003e 3, \"price\" =\u003e 300]\n]\n*/\n\n```\n\n#### filter(function($item))\n```php\n$products = [\n    [\"id\" =\u003e 1, \"name\" =\u003e \"product 1\", \"price\" =\u003e 100],\n    [\"id\" =\u003e 2, \"name\" =\u003e \"product 2\", \"price\" =\u003e 200],\n    [\"id\" =\u003e 3, \"name\" =\u003e \"product 3\", \"price\" =\u003e 300]\n];\n\n$collection = PhpCollection:collect($products)\n    -\u003efilter(function($item){\n        return $item[\"price\"] \u003e 100    \n    })\n    -\u003eall();\n\n/*\n[\n    [\"id\" =\u003e 2, \"name\" =\u003e \"product 2\", \"price\" =\u003e 200],\n    [\"id\" =\u003e 3, \"name\" =\u003e \"product 3\", \"price\" =\u003e 300]\n]\n*/\n\n```\n\n#### map(function($item))\n```php\n$products = [\n    [\"id\" =\u003e 1, \"name\" =\u003e \"product 1\", \"price\" =\u003e 100],\n    [\"id\" =\u003e 2, \"name\" =\u003e \"product 2\", \"price\" =\u003e 200],\n    [\"id\" =\u003e 3, \"name\" =\u003e \"product 3\", \"price\" =\u003e 300]\n];\n\n$collection = PhpCollection:collect($products)\n    -\u003emap(function($item){\n        $item[\"pricex2\"] = $item[\"price\"] * 2;\n        return $item;   \n    })\n    -\u003eall();\n\n/*\n[\n    [\"id\" =\u003e 1, \"name\" =\u003e \"product 1\", \"price\" =\u003e 100, \"pricex2\" =\u003e 200],\n    [\"id\" =\u003e 2, \"name\" =\u003e \"product 2\", \"price\" =\u003e 200, \"pricex2\" =\u003e 400],\n    [\"id\" =\u003e 3, \"name\" =\u003e \"product 3\", \"price\" =\u003e 300, \"pricex2\" =\u003e 600]\n]\n*/\n\n```\n\n#### keyBy($key | function($item))\n```php\n$products = [\n    [\"id\" =\u003e 16, \"name\" =\u003e \"product 1\", \"price\" =\u003e 100],\n    [\"id\" =\u003e 22, \"name\" =\u003e \"product 2\", \"price\" =\u003e 200],\n    [\"id\" =\u003e 31, \"name\" =\u003e \"product 3\", \"price\" =\u003e 300]\n];\n\n$collection = PhpCollection:collect($products)-\u003ekeyBy(\"id\")-\u003eall();\n\n/*\n[\n    16 =\u003e [\"id\" =\u003e 1, \"name\" =\u003e \"product 1\", \"price\" =\u003e 100, \"pricex2\" =\u003e 200],\n    22 =\u003e [\"id\" =\u003e 2, \"name\" =\u003e \"product 2\", \"price\" =\u003e 200, \"pricex2\" =\u003e 400],\n    31 =\u003e [\"id\" =\u003e 3, \"name\" =\u003e \"product 3\", \"price\" =\u003e 300, \"pricex2\" =\u003e 600]\n]\n*/\n\n```\n\n#### groupBy($key | function($item))\n```php\n$products = [\n    [\"id\" =\u003e 16, \"category_id\" =\u003e 1, \"name\" =\u003e \"product 1\", \"price\" =\u003e 100],\n    [\"id\" =\u003e 22, \"category_id\" =\u003e 2, \"name\" =\u003e \"product 2\", \"price\" =\u003e 200],\n    [\"id\" =\u003e 31, \"category_id\" =\u003e 2, \"name\" =\u003e \"product 3\", \"price\" =\u003e 300]\n];\n\n$collection = PhpCollection:collect($products)-\u003egroupBy(\"category_id\")-\u003eall();\n\n/*\n[\n    1 =\u003e [\n        [\"id\" =\u003e 16, \"category_id\" =\u003e 1, \"name\" =\u003e \"product 1\", \"price\" =\u003e 100]\n    ],\n    2 =\u003e [\n        [\"id\" =\u003e 22, \"category_id\" =\u003e 2, \"name\" =\u003e \"product 2\", \"price\" =\u003e 200],\n        [\"id\" =\u003e 31, \"category_id\" =\u003e 2, \"name\" =\u003e \"product 3\", \"price\" =\u003e 300]\n    ]   \n]\n*/\n\n```\n\n#### find(function($item))\n#### some($value | function($item)) OR contains($value | function($item))\n#### every(function($item))\n#### sort(function($item))\n#### sortBy($key, $asc = true)\n#### values()\n#### first()\n#### last()\n#### count()\n#### all() OR toArray()\n#### toJson()\n#### zip()\n\n## TODO\n#### dumpBrowser()\n#### dump()\n#### toCsv()\n#### toXml()\n#### shuffle()\n#### random()\n#### chunk()\n#### unique(\"\" | $key | function($item))\n#### collapse()\n#### diff($array)\n#### has($key)\n#### flip()\n#### min(\"\" | $key | function($item))\n#### max(\"\" | $key | function($item))\n#### reduce($function)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizica%2Fphp-collection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fizica%2Fphp-collection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizica%2Fphp-collection/lists"}