{"id":24518546,"url":"https://github.com/dperrymorrow/collection.php","last_synced_at":"2025-10-24T10:22:12.588Z","repository":{"id":140595351,"uuid":"45855162","full_name":"dperrymorrow/Collection.php","owner":"dperrymorrow","description":"PHP Library for manipulating collections of Objects, Associative Arrays, or Numeric Arrays","archived":false,"fork":false,"pushed_at":"2015-11-25T19:36:34.000Z","size":22,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-22T01:43:00.186Z","etag":null,"topics":[],"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/dperrymorrow.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}},"created_at":"2015-11-09T17:39:57.000Z","updated_at":"2022-02-20T11:06:17.000Z","dependencies_parsed_at":"2023-03-13T20:11:56.242Z","dependency_job_id":null,"html_url":"https://github.com/dperrymorrow/Collection.php","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dperrymorrow%2FCollection.php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dperrymorrow%2FCollection.php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dperrymorrow%2FCollection.php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dperrymorrow%2FCollection.php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dperrymorrow","download_url":"https://codeload.github.com/dperrymorrow/Collection.php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243719398,"owners_count":20336607,"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":[],"created_at":"2025-01-22T01:43:10.426Z","updated_at":"2025-10-24T10:22:07.559Z","avatar_url":"https://github.com/dperrymorrow.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Collection.php\n**Problem:** PHP can be annoying iterating over arrays over and over and cluttering up your code.\n\n**Solution:** Collection lets you do things like pluck, unique, filter, map, reject on a collection of items in PHP. \n\nCollection allows for chaining, which greatly reduces your code. Also the original collection is uneffected as you will see in the examples below, as a new Collection is returned on each method invoked.\n\n**A Collection will work with arrays of the following types.**\n\n- an Array of associative Arrays\n- an Array of Objects such as database result set\n- a single array of items such as strings or integers\n\n## Example\nSay you have an Associative Array of items like such. \n\n```php\n$people = [\n    ['name' =\u003e 'Jack',   'job' =\u003e 'Doctor',     'hobby' =\u003e 'Golf'], \n    ['name' =\u003e 'Mike',   'job' =\u003e 'Doctor',     'hobby' =\u003e 'Golf'], \n    ['name' =\u003e 'George', 'job' =\u003e 'Programmer', 'hobby' =\u003e 'Guitar'],\n    ['name' =\u003e 'Fred',   'job' =\u003e 'Accountant', 'hobby' =\u003e 'Travel'],\n    ['name' =\u003e 'David',  'job' =\u003e 'Painter',    'hobby' =\u003e 'Hiking'],\n];\n```\n\nYou could perform the following once converted into a Collection\n\n```php\n$collect = Collection::create($people);\n$collect-\u003esortOn('name')-\u003epluck('name')-\u003etoArray();\n// ['David', 'Fred', 'George', 'Jack', 'Mike']\n\n$collect-\u003ewhere(['profession' =\u003e 'Doctor'])-\u003epluck('name')-\u003efirst();\n// Jack\n\n$collect-\u003esortOn('hobby')-\u003epluck('hobby')-\u003etoArray();\n// ['Golf', 'Golf', 'Guitar', 'Hiking', 'Travel']\n\n$collect-\u003esortOn('hobby')-\u003epluck('hobby')-\u003eunique()-\u003etoArray();\n// ['Golf', 'Guitar', 'Hiking', 'Travel']\n```\n\nkeep reading for more detail and instructions of use of all the methods.\n\n#Methods\n| Methods | Parameters | Returns  |\n| ------  | -----------| ------------ |\n| [create](#create) | Array | a Collection object |\n| [toArray](#toArray) | none | An Array |\n| [pluck](#pluck) | String, or Array of keys | Collection of just those key values. |\n| [filter](#filter) | an anonymous function. | Collection where the return value of the function was true. |\n| [where](#where) | associative array | Collection of items that match the key values passed. |\n\n\n##Create \n\u003e ```Collection::create(Array)``` build a new Collection Object. \n\nAlias for ```new Collection(Array)``` using the static build Functiion allows for chaining off the contstructor\n\nBuild the Collection from your Array of Associative Arrays, Objects such as Database result set, or Numeric Array _(sequential)_\n\n```php\n$people = Collection::create([\n  ['name' =\u003e 'Jack',   'job' =\u003e 'Doctor',     'hobby' =\u003e 'Golf'], \n  ['name' =\u003e 'Mike',   'job' =\u003e 'Doctor',     'hobby' =\u003e 'Golf'], \n  ['name' =\u003e 'George', 'job' =\u003e 'Programmer', 'hobby' =\u003e 'Guitar'],\n  ['name' =\u003e 'Fred',   'job' =\u003e 'Accountant', 'hobby' =\u003e 'Travel'],\n  ['name' =\u003e 'David',  'job' =\u003e 'Painter',    'hobby' =\u003e 'Hiking'],\n]);\n```\n    \n##toArray \n\u003e ```-\u003etoArray()``` returns the Array that the Collection Object contains.\n    \n```php\n$people-\u003epluck('name')-\u003etoArray();\n// [Jack, Mike, George, Fred, David]\n```\n\n##Pluck \n\u003e ``` -\u003epluck(String or Array)``` pulls certain keys _(if Associative)_ or fields _(if a Collection of Objects)_\n\nwill throw an error if it is just a Numeric Array. Will remove any Null values collected using [rejectNull()](#rejectNull) method.\n\nAs with any keys passed to Collection functions, the key can represent a key in an associative Array, a attribute on an Object, or a Method on an Object.\n\n````php  \n$people-\u003epluck('profession')-\u003etoArray();\n//  [Doctor, Doctor, Programmer, Accountant, Painter]\n\n// with multiple keys.\n$people-\u003epluck(['name', 'profession'])-\u003etoArray();\n/*  \n[\n    [name =\u003e Jack, job =\u003e Doctor],\n    [name =\u003e Mike, job =\u003e Doctor],\n    [name =\u003e George, job =\u003e Programmer],\n    [name =\u003e Fred, job =\u003e Accountant],\n    [name =\u003e David, job =\u003e Painter]\n]\n*/\n```\n\n##Filter \n\u003e ```-\u003efilter(Function)``` Filters the Collection based on the Boolean return value of the Anonymous function passed. \n    \nIf your method returns true, the item will remain, if false, it will be removed from the returned Collection.\n\nAs with any Closure in PHP, you can access variables outside of the function using the use operator. \nSee [the PHP documentation on the subject.](http://php.net/manual/en/functions.anonymous.php)\n\n```php  \n// filter the collection to items wher name length is greater than 4.\n$people-\u003efilter(function($item) {\n  return strlen($item['name']) \u003e 4;\n})-\u003etoArray();\n/*\n[\n  [name =\u003e George, job =\u003e Programmer, hobby =\u003e Guitar],\n  [name =\u003e David, job =\u003e Painter, hobby =\u003e Hiking]\n]\n*/\n```\n\n##Where \n\u003e ```-\u003ewhere(Array)``` Returns a Collection consisting of items that meet the key values passed. \n\nWorks the same for Collections of Object attributes or Associative Array keys.\n```php    \n// find all the doctors.\n$people-\u003ewhere(['job' =\u003e 'Doctor'])-\u003etoArray();\n/*\n[\n  [name =\u003e Jack, job =\u003e Doctor, hobby =\u003e Golf],\n  [name =\u003e Mike, job =\u003e Doctor, hobby =\u003e Golf]\n]\n*/\n```\n\n##Find \n\u003e ```-\u003efind(Array)``` Returns an Associative Array, the first item found that matches the key vals passed. \n\nAlias for ```-\u003ewhere(Array)-\u003efirst()```\n\n```php\n// find the first doctor.\n$people-\u003efind(['job' =\u003e 'Doctor'])-\u003etoArray(); \n// [name =\u003e Jack, job =\u003e Doctor, hobby =\u003e Golf]\n```\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdperrymorrow%2Fcollection.php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdperrymorrow%2Fcollection.php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdperrymorrow%2Fcollection.php/lists"}