{"id":14966781,"url":"https://github.com/yii2mod/collection","last_synced_at":"2025-10-25T17:30:42.726Z","repository":{"id":57086992,"uuid":"56510669","full_name":"yii2mod/collection","owner":"yii2mod","description":"Basic collection library for Yii Framework 2.0","archived":false,"fork":false,"pushed_at":"2019-11-02T17:48:55.000Z","size":53,"stargazers_count":29,"open_issues_count":0,"forks_count":4,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-01-31T09:34:16.990Z","etag":null,"topics":["yii2","yii2-collection","yii2-collection-library","yii2-extension"],"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/yii2mod.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":"2016-04-18T13:37:31.000Z","updated_at":"2024-11-26T06:29:38.000Z","dependencies_parsed_at":"2022-08-24T14:00:38.426Z","dependency_job_id":null,"html_url":"https://github.com/yii2mod/collection","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2mod%2Fcollection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2mod%2Fcollection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2mod%2Fcollection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2mod%2Fcollection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yii2mod","download_url":"https://codeload.github.com/yii2mod/collection/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238183703,"owners_count":19430172,"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":["yii2","yii2-collection","yii2-collection-library","yii2-extension"],"created_at":"2024-09-24T13:36:56.107Z","updated_at":"2025-10-25T17:30:37.442Z","avatar_url":"https://github.com/yii2mod.png","language":"PHP","funding_links":["https://www.patreon.com/yii2mod"],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/yiisoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://avatars0.githubusercontent.com/u/993323\" height=\"100px\"\u003e\n    \u003c/a\u003e\n    \u003ch1 align=\"center\"\u003eCollection Extension for Yii 2\u003c/h1\u003e\n    \u003cbr\u003e\n\u003c/p\u003e\n\nThe `yii2mod\\collection\\Collection` class provides a fluent, convenient wrapper for working with arrays of data.\n\n[![Latest Stable Version](https://poser.pugx.org/yii2mod/collection/v/stable)](https://packagist.org/packages/yii2mod/collection) [![Total Downloads](https://poser.pugx.org/yii2mod/collection/downloads)](https://packagist.org/packages/yii2mod/collection) [![License](https://poser.pugx.org/yii2mod/collection/license)](https://packagist.org/packages/yii2mod/collection)\n[![Build Status](https://travis-ci.org/yii2mod/collection.svg?branch=master)](https://travis-ci.org/yii2mod/collection)\n\n\n## Support us\n\nDoes your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/yii2mod). \nAll pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.\n\n\nInstallation\n------------\n\nThe preferred way to install this extension is through [composer](http://getcomposer.org/download/).\n\nEither run\n\n```\nphp composer.phar require --prefer-dist yii2mod/collection \"*\"\n```\n\nor add\n\n```\n\"yii2mod/collection\": \"*\"\n```\n\nto the require section of your `composer.json` file.\n\nCreating Collections\n-------------------\n```php\n$collection = new Collection([1, 2, 3]);\n\n// or via `make` function\n\n$collection = Collection::make([1, 2, 3]);\n```\n\nAvailable Methods\n-------------------\n* **[all()](#all)**\n* **[avg()](#avg)**\n* **[chunk()](#chunk)**\n* **[collapse()](#collapse)**\n* **[combine()](#combine)**\n* **[contains()](#contains)**\n* **[count()](#count)**\n* **[diff()](#diff)**\n* **[each()](#each)**\n* **[every()](#every)**\n* **[except()](#except)**\n* **[filter()](#filter)**\n* **[first()](#first)**\n* **[last()](#last)**\n* **[flatten()](#flatten)**\n* **[flip()](#flip)**\n* **[forget()](#forget)**\n* **[forPage()](#forpage)**\n* **[get()](#get)**\n* **[groupBy()](#groupby)**\n* **[has()](#has)**\n* **[implode()](#implode)**\n* **[intersect()](#intersect)**\n* **[isEmpty()](#isempty)**\n* **[isNotEmpty()](#isnotempty)**\n* **[keyby()](#keyby)**\n* **[keys()](#keys)**\n* **[map()](#map)**\n* **[max()](#max)**\n* **[merge()](#merge)**\n* **[min()](#min)**\n* **[only()](#only)**\n* **[pluck()](#pluck)**\n* **[pop()](#pop)**\n* **[prepend()](#prepend)**\n* **[pull()](#pull)**\n* **[push()](#push)**\n* **[put()](#put)**\n* **[random()](#random)**\n* **[reduce()](#reduce)**\n* **[reject()](#reject)**\n* **[reverse()](#reverse)**\n* **[search()](#search)**\n* **[shift()](#shift)**\n* **[shuffle()](#shuffle)**\n* **[slice()](#slice)**\n* **[sort()](#sort)**\n* **[sortBy()](#sortby)** \n* **[sortByDesc()](#sortbydesc)** \n* **[splice()](#splice)**\n* **[sum()](#sum)**\n* **[take()](#take)**\n* **[toArray()](#toarray)**\n* **[tap()](#tap)**\n* **[toJson()](#tojson)**\n* **[transform()](#transform)**\n* **[unique()](#unique)**\n* **[uniqueStrict()](#uniquestrict)**\n* **[values()](#values)**\n* **[where()](#where)**\n* **[whereLoose()](#whereloose)**\n* **[whereIn()](#wherein)**\n* **[whereInLoose()](#whereinloose)**\n* **[zip()](#zip)**\n\nMethod Listing\n-------------------\n##### ```all()```\n\nThe `all` method simply returns the underlying array represented by the collection:\n```php\n$collection = new Collection([1, 2, 3]);\n$collection-\u003eall();\n// [1, 2, 3]\n```\n------\n\n##### ```avg()```\n\nThe `avg` method returns the average of all items in the collection:\n```php\n$collection = new Collection([1, 2, 3, 4, 5]);\n$collection-\u003eavg();\n// 3\n```\nIf the collection contains nested arrays or objects, you should pass a key to use for determining which values to calculate the average:\n```php\n$collection = new Collection([\n    ['id' =\u003e 1, 'price' =\u003e 150],\n    ['id' =\u003e 2, 'price' =\u003e 250],\n]);\n\n$collection-\u003eavg('price');\n\n// 200\n```\n------\n\n##### ```chunk()```\n\nThe `chunk` method breaks the collection into multiple, smaller collections of a given size:\n```php\n$collection = new Collection([1, 2, 3, 4, 5, 6, 7]);\n\n$chunks = $collection-\u003echunk(4);\n\n$chunks-\u003etoArray();\n\n// [[1, 2, 3, 4], [5, 6, 7]]\n```\n------\n\n##### ```collapse()```\n\nThe `collapse` method collapses a collection of arrays into a flat collection:\n```php\n$collection = new Collection([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);\n\n$collapsed = $collection-\u003ecollapse();\n\n$collapsed-\u003eall();\n\n// [1, 2, 3, 4, 5, 6, 7, 8, 9]\n```\n------\n\n##### ```combine()```\n\nCreate a collection by using this collection for keys and another for its values:\n```php\n$collection = new Collection(['name', 'age']);\n\n$combined = $collection-\u003ecombine(['George', 29]);\n\n$combined-\u003eall();\n\n// ['name' =\u003e 'George', 'age' =\u003e 29]\n```\n------\n\n##### ```contains()```\n\nThe `contains` method determines whether the collection contains a given item:\n```php\n$collection = new Collection(['city' =\u003e 'Alabama', 'country' =\u003e 'USA']);\n\n$collection-\u003econtains('Alabama');\n\n// true\n\n$collection-\u003econtains('New York');\n\n// false\n```\n\nYou may also pass a key / value pair to the contains method, which will determine if the given pair exists in the collection:\n\n```php\n$collection = new Collection([\n            ['city' =\u003e 'Alabama'],\n            ['city' =\u003e 'New York']\n        ]);\n\n$collection-\u003econtains('city', 'New York');\n\n// true\n```\n------\n\n##### ```count()```\n\nThe `count` method returns the total number of items in the collection:\n```php\n$collection = new Collection([1, 2, 3, 4, 5]);\n\n$collection-\u003ecount();\n\n// 5\n```\n------\n\n##### ```diff()```\n\nThe `diff` method compares the collection against another collection or a plain PHP array:\n```php\n$collection = new Collection([1, 2, 3, 4, 5]);\n\n$diff = $collection-\u003ediff([2, 4, 6, 8]);\n\n$diff-\u003eall();\n\n// [1, 3, 5]\n```\n------\n\n##### ```each()```\n\nThe `each` method iterates over the items in the collection and passes each item to a given callback:\n```php\n$collection = $collection-\u003eeach(function ($item, $key) {\n    if (/* some condition */) {\n        return false;\n    }\n});\n```\n------\n\n##### ```every()```\n\nThe `every` method creates a new collection consisting of every n-th element:\n```php\n$collection = new Collection(['a', 'b', 'c', 'd', 'e', 'f']);\n\n$collection-\u003eevery(4);\n\n// ['a', 'e']\n```\nYou may optionally pass offset as the second argument:\n```php\n$collection-\u003eevery(4, 1);\n\n// ['b', 'f']\n```\n------\n\n##### ```except()```\n\nGet all items except for those with the specified keys:\n```php\n$collection = new Collection(['id' =\u003e 1, 'name' =\u003e 'Desk', 'price' =\u003e 100, 'discount' =\u003e false]);\n\n$filtered = $collection-\u003eexcept(['price', 'discount']);\n\n$filtered-\u003eall();\n\n// ['id' =\u003e 1, 'name' =\u003e 'Desk']\n```\nFor the inverse of `except`, see the [only](#only) method.\n\n------\n\n##### ```filter()```\n\nThe `filter` method filters the collection by a given callback, keeping only those items that pass a given truth test:\n```php\n$collection = new Collection([1, 2, 3, 4]);\n\n$filtered = $collection-\u003efilter(function ($value, $key) {\n    return $value \u003e 2;\n});\n\n$filtered-\u003eall();\n\n// [3, 4]\n```\n------\n\n##### ```first()```\n\nThe `first` method returns the first element in the collection that passes a given truth test:\n```php\nCollection::make([1, 2, 3, 4])-\u003efirst(function ($key, $value) {\n    return $value \u003e 2;\n});\n\n// 3\n```\nYou may also call the first method with no arguments to get the first element in the collection. If the collection is empty, `null` is returned:\n```php\n$collection = new Collection([1, 2, 3, 4, 5]);\n\n$collection-\u003efirst();\n\n// 1\n```\n------\n\n##### ```last()```\n\nThe `last` method returns the last element in the collection that passes a given truth test:\n```php\nCollection::make([1, 2, 3, 4])-\u003elast(function ($key, $value) {\n    return $value \u003e 2;\n});\n\n// 4\n```\n\nYou may also call the `last` method with no arguments to get the last element in the collection. If the collection is empty, `null` is returned:\n\n```php\n$collection = new Collection([1, 2, 3, 4, 5]);\n\n$collection-\u003elast();\n\n// 5\n```\n------\n\n##### ```flatten()```\n\nThe `flatten` method flattens a multi-dimensional collection into a single dimension:\n```php\n$collection = new Collection(['language' =\u003e 'java', 'languages' =\u003e ['php', 'javascript']]);\n\n$collection-\u003eflatten();\n\n// ['java', 'php', 'javascript']\n```\n------\n\n##### ```flip()```\n\nThe `flip` method swaps the collection's keys with their corresponding values:\n```php\n$collection = new Collection(['firstName' =\u003e 'Igor', 'lastName' =\u003e 'Chepurnoy']);\n\n$collection-\u003eflip();\n\n// ['Igor' =\u003e 'firstName', 'Chepurnoy' =\u003e 'lastName']\n```\n------\n\n##### ```forget()```\n\nThe `forget` method removes an item from the collection by its key:\n```php\n$collection = new Collection(['firstName' =\u003e 'Igor', 'lastName' =\u003e 'Chepurnoy']);\n\n$collection-\u003eforget('firstName');\n\n$collection-\u003eall();\n\n// ['lastName' =\u003e 'Chepurnoy']\n```\n\u003e Unlike most other collection methods, `forget` does not return a new modified collection; it modifies the collection it is called on.\n\n------\n\n##### ```forPage()```\n\nThe `forPage` method returns a new collection containing the items that would be present on a given page number:\n\n```php\n$collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9]);\n\n$chunk = $collection-\u003eforPage(2, 3);\n\n$chunk-\u003eall();\n\n// [4, 5, 6]\n```\n\u003e The method requires the page number and the number of items to show per page, respectively.\n\n---------\n\n##### ```get()```\n\nGet an item from the collection by key:\n```php\n$collection = new Collection([\n'User' =\u003e [\n    'identity' =\u003e [\n        'id' =\u003e 1\n    ]\n]\n]);\n\n$collection-\u003eget('User.identity.id');\n\n// 1\n```\nYou may optionally pass a default value as the second argument:\n```php\n$collection-\u003eget('User.identity.email', false);\n\n// false\n```\n---------\n\n##### ```groupBy()```\n\nThe `groupBy` method groups the collection's items by a given key:\n```php\n$collection = new Collection([\n     ['id' =\u003e 'id_2', 'name' =\u003e 'Bob'],\n     ['id' =\u003e 'id_2', 'name' =\u003e 'John'],\n     ['id' =\u003e 'id_3', 'name' =\u003e 'Frank'],\n]);\n\n$grouped = $collection-\u003egroupBy('id');\n\n$grouped-\u003etoArray();\n\n/*\n[\n    'id_2' =\u003e [\n        ['id' =\u003e 'id_2', 'name' =\u003e 'Bob'],\n        ['id' =\u003e 'id_2', 'name' =\u003e 'John'],\n    ],\n    'id_3' =\u003e [\n        ['id' =\u003e 'id_3', 'name' =\u003e 'Frank'],\n    ],\n]\n*/\n```\nIn addition to passing a string key, you may also pass a callback. The callback should return the value you wish to key the group by:\n```php\n$grouped = $collection-\u003egroupBy(function ($item, $key) {\n    return substr($item['id'], -2);\n});\n\n/*\n[\n    '_2' =\u003e [\n        ['id' =\u003e 'id_2', 'name' =\u003e 'Bob'],\n        ['id' =\u003e 'id_2', 'name' =\u003e 'John'],\n    ],\n    '_3' =\u003e [\n        ['id' =\u003e 'id_3', 'name' =\u003e 'Frank'],\n    ],\n]\n*/\n```\n\n\n---------\n\n##### ```has()```\n\nThe `has` method determines if a given key exists in the collection:\n```php\n$collection = new Collection(['id' =\u003e 1, 'name' =\u003e 'Igor']);\n\n$collection-\u003ehas('id');\n\n// true\n\n$collection-\u003ehas('email');\n\n// false\n```\n---------\n\n##### ```implode()```\n\nConcatenate values of a given key as a string:\n```php\n$collection = new Collection([\n    ['account_id' =\u003e 1, 'name' =\u003e 'Ben'],\n    ['account_id' =\u003e 2, 'name' =\u003e 'Bob'],\n]);\n\n$collection-\u003eimplode('name', ', ');\n\n// Ben, Bob\n```\n\nIf the collection contains simple strings or numeric values, simply pass the \"glue\" as the only argument to the method:\n```php\nCollection::make(['Ben', 'Bob'])-\u003eimplode(' and ')\n\n// Ben and Bob\n```\n\n---------\n\n##### ```intersect()```\n\nThe `intersect` method removes any values that are not present in the given array or collection:\n```php\n$collection = new Collection(['php', 'python', 'ruby']);\n\n$intersect = $collection-\u003eintersect(['python', 'ruby', 'javascript']);\n\n$intersect-\u003eall();\n\n// [1 =\u003e 'python', 2 =\u003e 'ruby']\n```\n---------\n\n##### ```isEmpty()```\n\nThe `isEmpty` method returns true if the collection is empty; otherwise, false is returned:\n```php\n$collection = (new Collection([]))-\u003eisEmpty();\n\n// true\n```\n---------\n\n##### ```isNotEmpty()```\n\nThe `isNotEmpty` method returns true if the collection is not empty; otherwise, false is returned:\n\n```php\n$collection = (new Collection([]))-\u003eisNotEmpty();\n\n// false\n```\n---------\n\n##### ```keyBy()```\n\nKey an associative array by a field or using a callback:\n```php\n$collection = new Collection([\n    ['product_id' =\u003e '100', 'name' =\u003e 'desk'],\n    ['product_id' =\u003e '200', 'name' =\u003e 'chair'],\n]);\n\n$keyed = $collection-\u003ekeyBy('product_id');\n\n$keyed-\u003eall();\n\n/*\n  [\n     '100' =\u003e ['product_id' =\u003e '100', 'name' =\u003e 'desk'],\n     '200' =\u003e ['product_id' =\u003e '200', 'name' =\u003e 'chair'],\n  ]\n*/\n```\nYou may also pass your own callback, which should return the value to key the collection by:\n```php\n$collection = new Collection([\n    ['product_id' =\u003e '100', 'name' =\u003e 'desk'],\n    ['product_id' =\u003e '200', 'name' =\u003e 'chair'],\n]);\n\n$keyed = $collection-\u003ekeyBy(function ($item) {\n    return strtoupper($item['name']);\n});\n\n$keyed-\u003eall();\n\n/*\n  [\n    'DESK' =\u003e ['product_id' =\u003e '100', 'name' =\u003e 'desk'],\n    'CHAIR' =\u003e ['product_id' =\u003e '200', 'name' =\u003e 'chair'],\n  ]\n*/\n```\n\n---------\n\n##### ```keys()```\n\nThe `keys` method returns all of the collection's keys:\n```php\n$collection = new Collection([\n    'city' =\u003e 'New York',\n    'country' =\u003e 'USA'\n]);\n\n$collection-\u003ekeys();\n\n// ['city', 'country']\n```\n---------\n\n##### ```map()```\n\nThe `map` method iterates through the collection and passes each value to the given callback. The callback is free to modify the item and return it, thus forming a new collection of modified items:\n```php\n$collection = new Collection([1, 2, 3, 4, 5]);\n\n$multiplied = $collection-\u003emap(function ($item, $key) {\n    return $item * 2;\n});\n\n$multiplied-\u003eall();\n\n// [2, 4, 6, 8, 10]\n```\n\n\u003e Like most other collection methods, `map` returns a new collection instance; it does not modify the collection it is called on. If you want to transform the original collection, use the [transform](#transform) method.\n\n---------\n\n##### ```max()```\n\nGet the max value of a given key:\n```php\n$collection = new Collection([['foo' =\u003e 10], ['foo' =\u003e 20]]);\n$max = $collection-\u003emax('foo');\n\n// 20\n\n$collection = new Collection([1, 2, 3, 4, 5]);\n$max = $collection-\u003emax();\n\n// 5\n```\n---------\n\n##### ```merge()```\n\nMerge the collection with the given items:\n```php\n$collection = new Collection(['product_id' =\u003e 1, 'name' =\u003e 'Desk']);\n\n$merged = $collection-\u003emerge(['price' =\u003e 100, 'discount' =\u003e false]);\n\n$merged-\u003eall();\n\n// ['product_id' =\u003e 1, 'name' =\u003e 'Desk', 'price' =\u003e 100, 'discount' =\u003e false]\n```\n---------\n\n##### ```min()```\n\nGet the min value of a given key:\n```php\n$collection = new Collection([['foo' =\u003e 10], ['foo' =\u003e 20]]);\n$min = $collection-\u003emin('foo');\n\n// 10\n\n$collection = new Collection([1, 2, 3, 4, 5]);\n$min = $collection-\u003emin();\n\n// 1\n```\n---------\n\n##### ```only()```\n\nThe `only` method returns the items in the collection with the specified keys:\n```php\n$collection = new Collection(['product_id' =\u003e 1, 'name' =\u003e 'Desk', 'price' =\u003e 100, 'discount' =\u003e false]);\n\n$filtered = $collection-\u003eonly(['product_id', 'name']);\n\n$filtered-\u003eall();\n\n// ['product_id' =\u003e 1, 'name' =\u003e 'Desk']\n```\n---------\n\n\n##### ```pluck()```\n\nThe `pluck` method retrieves all of the collection values for a given key:\n```php\n$collection = new Collection([\n    ['product_id' =\u003e 'prod-100', 'name' =\u003e 'Desk'],\n    ['product_id' =\u003e 'prod-200', 'name' =\u003e 'Chair'],\n]);\n\n$plucked = $collection-\u003epluck('name');\n\n$plucked-\u003eall();\n\n// ['Desk', 'Chair']\n```\n\nYou may also specify how you wish the resulting collection to be keyed:\n\n```php\n$plucked = $collection-\u003epluck('name', 'product_id');\n\n$plucked-\u003eall();\n\n// ['prod-100' =\u003e 'Desk', 'prod-200' =\u003e 'Chair']\n```\n---------\n\n##### ```pop()```\n\nThe `pop` method removes and returns the last item from the collection:\n```php\n$collection = new Collection([1, 2, 3, 4, 5]);\n\n$collection-\u003epop();\n// 5\n\n$collection-\u003eall();\n\n// [1, 2, 3, 4]\n```\n---------\n\n##### ```prepend()```\n\nThe `prepend` method adds an item to the beginning of the collection:\n```php\n$collection = new Collection([1, 2, 3, 4, 5]);\n\n$collection-\u003eprepend(0);\n\n$collection-\u003eall();\n\n// [0, 1, 2, 3, 4, 5]\n```\n\nYou can optionally pass a second argument to set the key of the prepended item:\n\n```php\n$collection = Collection::make(['one' =\u003e 1, 'two' =\u003e 2]);\n\n$collection-\u003eprepend(0, 'zero');\n\n$collection-\u003eall();\n\n// ['zero' =\u003e 0, 'one' =\u003e 1, 'two', =\u003e 2]\n```\n\n---------\n\n##### ```pull()```\n\nThe `pull` method removes and returns an item from the collection by its key:\n```php\n$collection = new Collection(['product_id' =\u003e 'prod-100', 'name' =\u003e 'Desk']);\n\n$collection-\u003epull('name');\n\n// 'Desk'\n\n$collection-\u003eall();\n\n// ['product_id' =\u003e 'prod-100']\n```\n---------\n\n##### ```push()```\n\nPush an item onto the end of the collection:\n```php\n$collection = new Collection([1, 2, 3, 4]);\n\n$collection-\u003epush(5);\n\n$collection-\u003eall();\n\n// [1, 2, 3, 4, 5]\n```\n---------\n\n##### ```put()```\n\nPut an item in the collection by key:\n```php\n$collection = new Collection(['product_id' =\u003e 1, 'name' =\u003e 'Desk']);\n\n$collection-\u003eput('price', 100);\n\n$collection-\u003eall();\n\n// ['product_id' =\u003e 1, 'name' =\u003e 'Desk', 'price' =\u003e 100]\n```\n---------\n\n##### ```random()```\n\nThe `random` method returns a random item from the collection:\n```php\n$collection = new Collection([1, 2, 3, 4, 5]);\n\n$collection-\u003erandom();\n\n// 4 - (retrieved randomly)\n```\nYou may optionally pass an integer to random. If that integer is more than 1, a collection of items is returned:\n\n```php\n$random = $collection-\u003erandom(3);\n\n$random-\u003eall();\n\n// [2, 4, 5] - (retrieved randomly)\n```\n---------\n\n##### ```reduce()```\n\nThe `reduce` method reduces the collection to a single value, passing the result of each iteration into the subsequent iteration:\n```php\n$collection = new Collection([1, 2, 3]);\n\n$total = $collection-\u003ereduce(function ($carry, $item) {\n    return $carry + $item;\n});\n\n// 6\n```\nThe value for $carry on the first iteration is null; however, you may specify its initial value by passing a second argument to reduce:\n\n```php\n$collection-\u003ereduce(function ($carry, $item) {\n    return $carry + $item;\n}, 4);\n\n// 10\n```\n---------\n\n##### ```reject()```\n\nThe `reject` method filters the collection using the given callback. The callback should return true for any items it wishes to remove from the resulting collection:\n```php\n$collection = new Collection([1, 2, 3, 4]);\n\n$filtered = $collection-\u003ereject(function ($value, $key) {\n    return $value \u003e 2;\n});\n\n$filtered-\u003eall();\n\n// [1, 2]\n```\n---------\n\n##### ```reverse()```\n\nThe `reverse` method reverses the order of the collection's items:\n```php\n$collection = new Collection([1, 2, 3, 4, 5]);\n\n$reversed = $collection-\u003ereverse();\n\n$reversed-\u003eall();\n\n// [5, 4, 3, 2, 1]\n```\n---------\n\n##### ```search()```\n\nSearch the collection for a given value and return the corresponding key if successful:\n```php\n$collection = new Collection([2, 4, 6, 8]);\n\n$collection-\u003esearch(4);\n\n// 1\n```\nThe search is done using a \"loose\" comparison. To use strict comparison, pass true as the second argument to the method:\n```php\n$collection-\u003esearch('4', true);\n\n// false\n```\n---------\n\n##### ```shift()```\n\nThe `shift` method removes and returns the first item from the collection:\n```php\n$collection = new Collection([1, 2, 3, 4, 5]);\n\n$collection-\u003eshift();\n\n// 1\n\n$collection-\u003eall();\n\n// [2, 3, 4, 5]\n```\n---------\n\n##### ```shuffle()```\n\nShuffle the items in the collection:\n```php\n$collection = new Collection([1, 2, 3, 4, 5]);\n\n$shuffled = $collection-\u003eshuffle();\n\n$shuffled-\u003eall();\n\n// [3, 2, 5, 1, 4] // (generated randomly)\n```\n---------\n\n##### ```slice()```\n\nSlice the underlying collection array:\n```php\n$collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);\n\n$slice = $collection-\u003eslice(4);\n\n$slice-\u003eall();\n\n// [5, 6, 7, 8, 9, 10]\n```\nIf you would like to limit the size of the returned slice, pass the desired size as the second argument to the method:\n\n```php\n$slice = $collection-\u003eslice(4, 2);\n\n$slice-\u003eall();\n\n// [5, 6]\n```\n---------\n\n##### ```sort()```\n\nSort through each item with a callback:\n```php\n$collection = new Collection([5, 3, 1, 2, 4]);\n\n$sorted = $collection-\u003esort();\n\n$sorted-\u003evalues()-\u003eall();\n\n// [1, 2, 3, 4, 5]\n```\n---------\n\n##### ```sortBy()```\n\nSort the collection using the given callback:\n```php\n$collection = new Collection([\n    ['name' =\u003e 'Desk', 'price' =\u003e 200],\n    ['name' =\u003e 'Chair', 'price' =\u003e 100],\n    ['name' =\u003e 'Bookcase', 'price' =\u003e 150],\n]);\n\n$sorted = $collection-\u003esortBy('price');\n\n$sorted-\u003evalues()-\u003eall();\n\n/*\n    [\n        ['name' =\u003e 'Chair', 'price' =\u003e 100],\n        ['name' =\u003e 'Bookcase', 'price' =\u003e 150],\n        ['name' =\u003e 'Desk', 'price' =\u003e 200],\n    ]\n*/\n```\nYou can also pass your own callback to determine how to sort the collection values:\n```php\n$collection = new Collection([\n    ['name' =\u003e 'Desk', 'colors' =\u003e ['Black', 'Mahogany']],\n    ['name' =\u003e 'Chair', 'colors' =\u003e ['Black']],\n    ['name' =\u003e 'Bookcase', 'colors' =\u003e ['Red', 'Beige', 'Brown']],\n]);\n\n$sorted = $collection-\u003esortBy(function ($product, $key) {\n    return count($product['colors']);\n});\n\n$sorted-\u003evalues()-\u003eall();\n\n/*\n    [\n        ['name' =\u003e 'Chair', 'colors' =\u003e ['Black']],\n        ['name' =\u003e 'Desk', 'colors' =\u003e ['Black', 'Mahogany']],\n        ['name' =\u003e 'Bookcase', 'colors' =\u003e ['Red', 'Beige', 'Brown']],\n    ]\n*/\n```\n---------\n\n##### ```sortByDesc()```\nThis method has the same signature as the [sortBy()](#sortby) method, but will sort the collection in the opposite order.\n\n---------\n\n##### ```splice()```\n\nSplice a portion of the underlying collection array:\n```php\n$collection = new Collection([1, 2, 3, 4, 5]);\n\n$chunk = $collection-\u003esplice(2);\n\n$chunk-\u003eall();\n\n// [3, 4, 5]\n\n$collection-\u003eall();\n\n// [1, 2]\n```\nYou may pass a second argument to limit the size of the resulting chunk:\n```php\n$collection = new Collection([1, 2, 3, 4, 5]);\n\n$chunk = $collection-\u003esplice(2, 1);\n\n$chunk-\u003eall();\n\n// [3]\n\n$collection-\u003eall();\n\n// [1, 2, 4, 5]\n```\nIn addition, you can pass a third argument containing the new items to replace the items removed from the collection:\n```php\n$collection = new Collection([1, 2, 3, 4, 5]);\n\n$chunk = $collection-\u003esplice(2, 1, [10, 11]);\n\n$chunk-\u003eall();\n\n// [3]\n\n$collection-\u003eall();\n\n// [1, 2, 10, 11, 4, 5]\n```\n---------\n\n##### ```sum()```\n\nGet the sum of the given values:\n```php\n$collection = new Collection([1, 2, 3]);\n        \n$collection-\u003esum();\n\n// 6\n```\nIf the collection contains nested arrays or objects, you should pass a key to use for determining which values to sum:\n```php\n$collection = new Collection([\n    ['name' =\u003e 'Books', 'countOfProduct' =\u003e 100],\n    ['name' =\u003e 'Chairs', 'countOfProduct' =\u003e 200],\n]);\n\n$collection-\u003esum('countOfProduct');\n\n// 300\n```\nIn addition, you may pass your own callback to determine which values of the collection to sum:\n```php\n$collection = new Collection([\n    ['name' =\u003e 'Chair', 'colors' =\u003e ['Black']],\n    ['name' =\u003e 'Desk', 'colors' =\u003e ['Black', 'Mahogany']],\n    ['name' =\u003e 'Bookcase', 'colors' =\u003e ['Red', 'Beige', 'Brown']],\n]);\n\n$collection-\u003esum(function ($product) {\n    return count($product['colors']);\n});\n\n// 6\n```\n---------\n\n##### ```take()```\n\nTake the first or last {$limit} items:\n```php\n$collection = new Collection([0, 1, 2, 3, 4, 5]);\n\n$chunk = $collection-\u003etake(3);\n\n$chunk-\u003eall();\n\n// [0, 1, 2]\n```\nYou may also pass a negative integer to take the specified amount of items from the end of the collection:\n```php\n$collection = new Collection([0, 1, 2, 3, 4, 5]);\n\n$chunk = $collection-\u003etake(-2);\n\n$chunk-\u003eall();\n\n// [4, 5]\n```\n---------\n\n##### ```toArray()```\n\nGet the collection of items as a plain array:\n```php\n$collection = new Collection('name');\n\n$collection-\u003etoArray();\n\n/*\n    ['name']\n*/\n```\n\u003e [toArray()](#toArray) also converts all of its nested objects to an array. If you want to get the underlying array as is, use the [all()](#all) method instead.\n\n---------\n\n##### ```tap()```\n\nThe `tap` method passes the collection to the given callback, allowing you to \"tap\" into the collection at a specific point and do something with the items while not affecting the collection itself:\n\n```php\n$collection = new Collection([2, 4, 3, 1, 5]);\n$result = $collection-\u003esort()\n    -\u003etap(function ($collection) {\n        // Values after sorting\n        var_dump($collection-\u003evalues()-\u003etoArray());\n    })\n    -\u003eshift();\n// 1\n```\n---------\n\n##### ```toJson()```\n\nGet the collection of items as JSON:\n```php\n$collection = new Collection(['name' =\u003e 'Desk', 'price' =\u003e 200]);\n\n$collection-\u003etoJson();\n\n// '{\"name\":\"Desk\",\"price\":200}'\n```\n---------\n\n##### ```transform()```\n\nTransform each item in the collection using a callback:\n```php\n$collection = new Collection([1, 2, 3, 4, 5]);\n\n$collection-\u003etransform(function ($item, $key) {\n    return $item * 2;\n});\n\n$collection-\u003eall();\n\n// [2, 4, 6, 8, 10]\n```\n\u003e Unlike most other collection methods, [transform()](#transform) modifies the collection itself. If you wish to create a new collection instead, use the [map()](#map) method.\n\n---------\n\n##### ```unique()```\n\nReturn only unique items from the collection array:\n```php\n$collection = new Collection([1, 1, 2, 2, 3, 4, 2]);\n\n$unique = $collection-\u003eunique();\n\n$unique-\u003evalues()-\u003eall();\n\n// [1, 2, 3, 4]\n```\n\nThe returned collection keeps the original array keys. In this example we used the [values()](#values) method to reset the keys to consecutively numbered indexes.\n\nWhen dealing with nested arrays or objects, you may specify the key used to determine uniqueness:\n\n```php\n$collection = new Collection([\n    ['name' =\u003e 'iPhone 6', 'brand' =\u003e 'Apple', 'type' =\u003e 'phone'],\n    ['name' =\u003e 'iPhone 5', 'brand' =\u003e 'Apple', 'type' =\u003e 'phone'],\n    ['name' =\u003e 'Apple Watch', 'brand' =\u003e 'Apple', 'type' =\u003e 'watch'],\n    ['name' =\u003e 'Galaxy S6', 'brand' =\u003e 'Samsung', 'type' =\u003e 'phone'],\n    ['name' =\u003e 'Galaxy Gear', 'brand' =\u003e 'Samsung', 'type' =\u003e 'watch'],\n]);\n\n$unique = $collection-\u003eunique('brand');\n\n$unique-\u003evalues()-\u003eall();\n\n/*\n    [\n        ['name' =\u003e 'iPhone 6', 'brand' =\u003e 'Apple', 'type' =\u003e 'phone'],\n        ['name' =\u003e 'Galaxy S6', 'brand' =\u003e 'Samsung', 'type' =\u003e 'phone'],\n    ]\n*/\n```\nYou may also pass your own callback to determine item uniqueness:\n\n```php\n$unique = $collection-\u003eunique(function ($item) {\n    return $item['brand'].$item['type'];\n});\n\n$unique-\u003evalues()-\u003eall();\n\n/*\n    [\n        ['name' =\u003e 'iPhone 6', 'brand' =\u003e 'Apple', 'type' =\u003e 'phone'],\n        ['name' =\u003e 'Apple Watch', 'brand' =\u003e 'Apple', 'type' =\u003e 'watch'],\n        ['name' =\u003e 'Galaxy S6', 'brand' =\u003e 'Samsung', 'type' =\u003e 'phone'],\n        ['name' =\u003e 'Galaxy Gear', 'brand' =\u003e 'Samsung', 'type' =\u003e 'watch'],\n    ]\n*/\n```\n---------\n\n##### ```uniqueStrict()```\n\nThis method has the same signature as the `unique` method; however, all values are compared using \"strict\" comparisons.\n\n---------\n\n##### ```values()```\n\nReset the keys on the underlying array:\n```php\n$collection = new Collection([\n    10 =\u003e ['product' =\u003e 'Desk', 'price' =\u003e 200],\n    11 =\u003e ['product' =\u003e 'Desk', 'price' =\u003e 200]\n]);\n\n$values = $collection-\u003evalues();\n\n$values-\u003eall();\n\n/*\n    [\n        0 =\u003e ['product' =\u003e 'Desk', 'price' =\u003e 200],\n        1 =\u003e ['product' =\u003e 'Desk', 'price' =\u003e 200],\n    ]\n*/\n```\n---------\n\n##### ```where()```\n\nThe `where` method filters the collection by a given key / value pair:\n```php\n$collection = new Collection([\n    ['product' =\u003e 'Desk', 'price' =\u003e 200],\n    ['product' =\u003e 'Chair', 'price' =\u003e 100],\n    ['product' =\u003e 'Bookcase', 'price' =\u003e 150],\n    ['product' =\u003e 'Door', 'price' =\u003e 100],\n]);\n\n$filtered = $collection-\u003ewhere('price', 100);\n\n$filtered-\u003eall();\n\n/*\n  [\n     ['product' =\u003e 'Chair', 'price' =\u003e 100],\n     ['product' =\u003e 'Door', 'price' =\u003e 100],\n  ]\n*/\n\n```\nThe [where()](#where) method uses strict comparisons when checking item values. Use the whereLoose method to filter using [whereLoose()](#whereloose) comparisons.\n\n---------\n\n##### ```whereLoose()```\n\nThis method has the same signature as the [where()](#where) method; however, all values are compared using \"loose\" comparisons.\n\n---------\n\n##### ```whereIn()```\n\nThe `whereIn` method filters the collection by a given key / value contained within the given array.\n```php\n$collection = new Collection([\n    ['product' =\u003e 'Desk', 'price' =\u003e 200],\n    ['product' =\u003e 'Chair', 'price' =\u003e 100],\n    ['product' =\u003e 'Bookcase', 'price' =\u003e 150],\n    ['product' =\u003e 'Door', 'price' =\u003e 100],\n]);\n\n$filtered = $collection-\u003ewhereIn('price', [150, 200]);\n\n$filtered-\u003eall();\n\n/*\n   [\n      ['product' =\u003e 'Bookcase', 'price' =\u003e 150],\n      ['product' =\u003e 'Desk', 'price' =\u003e 200],\n   ]\n*/\n```\nThe [whereIn()](#wherein) method uses strict comparisons when checking item values. Use the [whereInLoose()](#whereinloose) method to filter using \"loose\" comparisons.\n\n---------\n\n##### ```whereInLoose()```\n\nThis method has the same signature as the [whereIn()](#wherein) method; however, all values are compared using \"loose\" comparisons.\n\n---------\n\n##### ```zip()```\n\nThe `zip` method merges together the values of the given array with the values of the collection at the corresponding index:\n\n```php\n$collection = new Collection(['Chair', 'Desk']);\n\n$zipped = $collection-\u003ezip([100, 200]);\n\n$zipped-\u003eall();\n\n// [['Chair', 100], ['Desk', 200]]\n```\n---------\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyii2mod%2Fcollection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyii2mod%2Fcollection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyii2mod%2Fcollection/lists"}