{"id":24764078,"url":"https://github.com/stillat/collection","last_synced_at":"2025-10-11T13:30:15.854Z","repository":{"id":57059956,"uuid":"46645856","full_name":"Stillat/Collection","owner":"Stillat","description":"A reusable standalone Collection class, pulled from Laravel.","archived":false,"fork":false,"pushed_at":"2016-09-05T18:20:49.000Z","size":39,"stargazers_count":5,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-23T18:46:50.354Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/Stillat.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-22T04:10:01.000Z","updated_at":"2020-10-20T10:23:40.000Z","dependencies_parsed_at":"2022-08-24T14:53:29.653Z","dependency_job_id":null,"html_url":"https://github.com/Stillat/Collection","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/Stillat/Collection","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Stillat%2FCollection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Stillat%2FCollection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Stillat%2FCollection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Stillat%2FCollection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Stillat","download_url":"https://codeload.github.com/Stillat/Collection/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Stillat%2FCollection/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279007341,"owners_count":26084282,"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","status":"online","status_checked_at":"2025-10-11T02:00:06.511Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":[],"created_at":"2025-01-28T21:32:30.458Z","updated_at":"2025-10-11T13:30:15.560Z","avatar_url":"https://github.com/Stillat.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# __IMPORTANT__: No Longer Maintained\n\nThis repository is no longer maintained. However, you should totally check out a similar effort over at https://github.com/tightenco/collect (it's what I am going to be using).\n\n# What is this?\n\nThis is Laravel's Collection helper class (PHP arrays on steroids) that has been pulled from Laravel that can be used all by itself. It doesn't contain any unneeded helper functions, extra classes or service providers. It is just the Collection class. In addition, it doesn't use the `Illuminate` namespace. Other than that, everything else is pretty much the same:\n\n    $collection = collect(['taylor', 'abigail', null])-\u003emap(function ($name) {\n        return strtoupper($name);\n    })\n    -\u003ereject(function ($name) {\n        return empty($name);\n    });\n\n## Installing\n\nInstall `stillat/collection` using Composer.\n\n```\n$ composer require stillat/collection\n```\n\nAfter Composer does its thing, you should now have the collection helpers available for your project! Continue on to learn all about it.\n\n# Collections\n\n\u003eNote: This documentation comes directly from the [Laravel Docs](https://github.com/laravel/docs) repository. Some tables have been reformatted to make it nicer to look at in GitHub, but that is about it.\n\n- [Introduction](#introduction)\n- [Creating Collections](#creating-collections)\n- [Available Methods](#available-methods)\n- [When will updates be made to this library?](#updates)\n- [Can I contribute improvements/fixes?](#howtocontribute)\n- [Motivation/Why is this a thing?](#motivation)\n\n\u003ca name=\"introduction\"\u003e\u003c/a\u003e\n## Introduction\n\nThe `Collection\\Collection` class provides a fluent, convenient wrapper for working with arrays of data. For example, check out the following code. We'll use the `collect` helper to create a new collection instance from the array, run the `strtoupper` function on each element, and then remove all empty elements:\n\n    $collection = collect(['taylor', 'abigail', null])-\u003emap(function ($name) {\n        return strtoupper($name);\n    })\n    -\u003ereject(function ($name) {\n        return empty($name);\n    });\n\n\nAs you can see, the `Collection` class allows you to chain its methods to perform fluent mapping and reducing of the underlying array. In general, every `Collection` method returns an entirely new `Collection` instance.\n\n\u003ca name=\"creating-collections\"\u003e\u003c/a\u003e\n## Creating Collections\n\nAs mentioned above, the `collect` helper returns a new `Collection\\Collection` instance for the given array. So, creating a collection is as simple as:\n\n    $collection = collect([1, 2, 3]);\n\n\u003ca name=\"available-methods\"\u003e\u003c/a\u003e\n## Available Methods\n\nFor the remainder of this documentation, we'll discuss each method available on the `Collection` class. Remember, all of these methods may be chained for fluently manipulating the underlying array. Furthermore, almost every method returns a new `Collection` instance, allowing you to preserve the original copy of the collection when necessary.\n\nYou may select any method from this table to see an example of its usage:\n\n| Method | ... | ... |\n|---|---|---|\n| [all](#method-all) | [intersect](#method-intersect) | [search](#method-search) |\n| [avg](#method-avg) | [isEmpty](#method-isempty) | [shift](#method-shift) |\n| [chunk](#method-chunk) | [keyBy](#method-keyby) | [shuffle](#method-shuffle) |\n| [collapse](#method-collapse) | [keys](#method-keys) | [slice](#method-slice) |\n| [contains](#method-contains) | [last](#method-last) | [sort](#method-sort) |\n| [count](#method-count) | [map](#method-map) | [sortBy](#method-sortby) |\n| [diff](#method-diff) | [max](#method-max) | [sortByDesc](#method-sortbydesc) |\n| [each](#method-each) | [merge](#method-merge) | [splice](#method-splice) |\n| [every](#method-every) | [min](#method-min) | [sum](#method-sum) |\n| [except](#method-except) | [only](#method-only) | [take](#method-take) |\n| [filter](#method-filter) | [pluck](#method-pluck) | [toArray](#method-toarray) |\n| [first](#method-first) | [pop](#method-pop) | [toJson](#method-tojson) |\n| [flatten](#method-flatten) | [prepend](#method-prepend) | [transform](#method-transform) |\n| [flip](#method-flip) | [pull](#method-pull) | [unique](#method-unique) |\n| [forget](#method-forget) | [push](#method-push) | [values](#method-values) |\n| [forPage](#method-forpage) | [put](#method-put) | [where](#method-where) |\n| [get](#method-get) | [random](#method-random) | [whereLoose](#method-whereloose) |\n| [groupBy](#method-groupby) | [reduce](#method-reduce) | [zip](#method-zip) |\n| [has](#method-has) | [reject](#method-reject) | |\n| [implode](#method-implode) | [reverse](#method-reverse) | |\n\n\n\u003ca name=\"method-listing\"\u003e\u003c/a\u003e\n## Method Listing\n\n\u003ca name=\"method-all\"\u003e\u003c/a\u003e\n#### `all()`\n\nThe `all` method simply returns the underlying array represented by the collection:\n\n    collect([1, 2, 3])-\u003eall();\n\n    // [1, 2, 3]\n\n\u003ca name=\"method-avg\"\u003e\u003c/a\u003e\n#### `avg()`\n\nThe `avg` method returns the average of all items in the collection:\n\n    collect([1, 2, 3, 4, 5])-\u003eavg();\n\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\n    $collection = collect([\n        ['name' =\u003e 'JavaScript: The Good Parts', 'pages' =\u003e 176],\n        ['name' =\u003e 'JavaScript: The Definitive Guide', 'pages' =\u003e 1096],\n    ]);\n\n    $collection-\u003eavg('pages');\n\n    // 636\n\n\u003ca name=\"method-chunk\"\u003e\u003c/a\u003e\n#### `chunk()`\n\nThe `chunk` method breaks the collection into multiple, smaller collections of a given size:\n\n    $collection = collect([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\u003ca name=\"method-collapse\"\u003e\u003c/a\u003e\n#### `collapse()`\n\nThe `collapse` method collapses a collection of arrays into a flat collection:\n\n    $collection = collect([[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\u003ca name=\"method-contains\"\u003e\u003c/a\u003e\n#### `contains()`\n\nThe `contains` method determines whether the collection contains a given item:\n\n    $collection = collect(['name' =\u003e 'Desk', 'price' =\u003e 100]);\n\n    $collection-\u003econtains('Desk');\n\n    // true\n\n    $collection-\u003econtains('New York');\n\n    // false\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    $collection = collect([\n        ['product' =\u003e 'Desk', 'price' =\u003e 200],\n        ['product' =\u003e 'Chair', 'price' =\u003e 100],\n    ]);\n\n    $collection-\u003econtains('product', 'Bookcase');\n\n    // false\n\nFinally, you may also pass a callback to the `contains` method to perform your own truth test:\n\n    $collection = collect([1, 2, 3, 4, 5]);\n\n    $collection-\u003econtains(function ($key, $value) {\n        return $value \u003e 5;\n    });\n\n    // false\n\n\u003ca name=\"method-count\"\u003e\u003c/a\u003e\n#### `count()`\n\nThe `count` method returns the total number of items in the collection:\n\n    $collection = collect([1, 2, 3, 4]);\n\n    $collection-\u003ecount();\n\n    // 4\n\n\u003ca name=\"method-diff\"\u003e\u003c/a\u003e\n#### `diff()`\n\nThe `diff` method compares the collection against another collection or a plain PHP `array`:\n\n    $collection = collect([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\u003ca name=\"method-each\"\u003e\u003c/a\u003e\n#### `each()`\n\nThe `each` method iterates over the items in the collection and passes each item to a given callback:\n\n    $collection = $collection-\u003eeach(function ($item, $key) {\n        //\n    });\n\nReturn `false` from your callback to break out of the loop:\n\n    $collection = $collection-\u003eeach(function ($item, $key) {\n        if (/* some condition */) {\n            return false;\n        }\n    });\n\n\u003ca name=\"method-every\"\u003e\u003c/a\u003e\n#### `every()`\n\nThe `every` method creates a new collection consisting of every n-th element:\n\n    $collection = collect(['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\n    $collection-\u003eevery(4, 1);\n\n    // ['b', 'f']\n\n\u003ca name=\"method-except\"\u003e\u003c/a\u003e\n#### `except()`\n\nThe `except` method returns all items in the collection except for those with the specified keys:\n\n    $collection = collect(['product_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    // ['product_id' =\u003e 1, 'name' =\u003e 'Desk']\n\nFor the inverse of `except`, see the [only](#method-only) method.\n\n\u003ca name=\"method-filter\"\u003e\u003c/a\u003e\n#### `filter()`\n\nThe `filter` method filters the collection by a given callback, keeping only those items that pass a given truth test:\n\n    $collection = collect([1, 2, 3, 4]);\n\n    $filtered = $collection-\u003efilter(function ($item) {\n        return $item \u003e 2;\n    });\n\n    $filtered-\u003eall();\n\n    // [3, 4]\n\nFor the inverse of `filter`, see the [reject](#method-reject) method.\n\n\u003ca name=\"method-first\"\u003e\u003c/a\u003e\n#### `first()`\n\nThe `first` method returns the first element in the collection that passes a given truth test:\n\n    collect([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\n    collect([1, 2, 3, 4])-\u003efirst();\n\n    // 1\n\n\u003ca name=\"method-flatten\"\u003e\u003c/a\u003e\n#### `flatten()`\n\nThe `flatten` method flattens a multi-dimensional collection into a single dimension:\n\n    $collection = collect(['name' =\u003e 'taylor', 'languages' =\u003e ['php', 'javascript']]);\n\n    $flattened = $collection-\u003eflatten();\n\n    $flattened-\u003eall();\n\n    // ['taylor', 'php', 'javascript'];\n\n\u003ca name=\"method-flip\"\u003e\u003c/a\u003e\n#### `flip()`\n\nThe `flip` method swaps the collection's keys with their corresponding values:\n\n    $collection = collect(['name' =\u003e 'taylor', 'framework' =\u003e 'laravel']);\n\n    $flipped = $collection-\u003eflip();\n\n    $flipped-\u003eall();\n\n    // ['taylor' =\u003e 'name', 'laravel' =\u003e 'framework']\n\n\u003ca name=\"method-forget\"\u003e\u003c/a\u003e\n#### `forget()`\n\nThe `forget` method removes an item from the collection by its key:\n\n    $collection = collect(['name' =\u003e 'taylor', 'framework' =\u003e 'laravel']);\n\n    $collection-\u003eforget('name');\n\n    $collection-\u003eall();\n\n    // [framework' =\u003e 'laravel']\n\n\u003e **Note:** Unlike most other collection methods, `forget` does not return a new modified collection; it modifies the collection it is called on.\n\n\u003ca name=\"method-forpage\"\u003e\u003c/a\u003e\n#### `forPage()`\n\nThe `forPage` method returns a new collection containing the items that would be present on a given page number:\n\n    $collection = collect([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\nThe method requires the page number and the number of items to show per page, respectively.\n\n\u003ca name=\"method-get\"\u003e\u003c/a\u003e\n#### `get()`\n\nThe `get` method returns the item at a given key. If the key does not exist, `null` is returned:\n\n    $collection = collect(['name' =\u003e 'taylor', 'framework' =\u003e 'laravel']);\n\n    $value = $collection-\u003eget('name');\n\n    // taylor\n\nYou may optionally pass a default value as the second argument:\n\n    $collection = collect(['name' =\u003e 'taylor', 'framework' =\u003e 'laravel']);\n\n    $value = $collection-\u003eget('foo', 'default-value');\n\n    // default-value\n\nYou may even pass a callback as the default value. The result of the callback will be returned if the specified key does not exist:\n\n    $collection-\u003eget('email', function () {\n        return 'default-value';\n    });\n\n    // default-value\n\n\u003ca name=\"method-groupby\"\u003e\u003c/a\u003e\n#### `groupBy()`\n\nThe `groupBy` method groups the collection's items by a given key:\n\n    $collection = collect([\n        ['account_id' =\u003e 'account-x10', 'product' =\u003e 'Chair'],\n        ['account_id' =\u003e 'account-x10', 'product' =\u003e 'Bookcase'],\n        ['account_id' =\u003e 'account-x11', 'product' =\u003e 'Desk'],\n    ]);\n\n    $grouped = $collection-\u003egroupBy('account_id');\n\n    $grouped-\u003etoArray();\n\n    /*\n        [\n            'account-x10' =\u003e [\n                ['account_id' =\u003e 'account-x10', 'product' =\u003e 'Chair'],\n                ['account_id' =\u003e 'account-x10', 'product' =\u003e 'Bookcase'],\n            ],\n            'account-x11' =\u003e [\n                ['account_id' =\u003e 'account-x11', 'product' =\u003e 'Desk'],\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\n    $grouped = $collection-\u003egroupBy(function ($item, $key) {\n        return substr($item['account_id'], -3);\n    });\n\n    $grouped-\u003etoArray();\n\n    /*\n        [\n            'x10' =\u003e [\n                ['account_id' =\u003e 'account-x10', 'product' =\u003e 'Chair'],\n                ['account_id' =\u003e 'account-x10', 'product' =\u003e 'Bookcase'],\n            ],\n            'x11' =\u003e [\n                ['account_id' =\u003e 'account-x11', 'product' =\u003e 'Desk'],\n            ],\n        ]\n    */\n\n\u003ca name=\"method-has\"\u003e\u003c/a\u003e\n#### `has()`\n\nThe `has` method determines if a given key exists in the collection:\n\n    $collection = collect(['account_id' =\u003e 1, 'product' =\u003e 'Desk']);\n\n    $collection-\u003ehas('email');\n\n    // false\n\n\u003ca name=\"method-implode\"\u003e\u003c/a\u003e\n#### `implode()`\n\nThe `implode` method joins the items in a collection. Its arguments depend on the type of items in the collection.\n\nIf the collection contains arrays or objects, you should pass the key of the attributes you wish to join, and the \"glue\" string you wish to place between the values:\n\n    $collection = collect([\n        ['account_id' =\u003e 1, 'product' =\u003e 'Desk'],\n        ['account_id' =\u003e 2, 'product' =\u003e 'Chair'],\n    ]);\n\n    $collection-\u003eimplode('product', ', ');\n\n    // Desk, Chair\n\nIf the collection contains simple strings or numeric values, simply pass the \"glue\" as the only argument to the method:\n\n    collect([1, 2, 3, 4, 5])-\u003eimplode('-');\n\n    // '1-2-3-4-5'\n\n\u003ca name=\"method-intersect\"\u003e\u003c/a\u003e\n#### `intersect()`\n\nThe `intersect` method removes any values that are not present in the given `array` or collection:\n\n    $collection = collect(['Desk', 'Sofa', 'Chair']);\n\n    $intersect = $collection-\u003eintersect(['Desk', 'Chair', 'Bookcase']);\n\n    $intersect-\u003eall();\n\n    // [0 =\u003e 'Desk', 2 =\u003e 'Chair']\n\nAs you can see, the resulting collection will preserve the original collection's keys.\n\n\u003ca name=\"method-isempty\"\u003e\u003c/a\u003e\n#### `isEmpty()`\n\nThe `isEmpty` method returns `true` if the collection is empty; otherwise, `false` is returned:\n\n    collect([])-\u003eisEmpty();\n\n    // true\n\n\u003ca name=\"method-keyby\"\u003e\u003c/a\u003e\n#### `keyBy()`\n\nKeys the collection by the given key:\n\n    $collection = collect([\n        ['product_id' =\u003e 'prod-100', 'name' =\u003e 'desk'],\n        ['product_id' =\u003e 'prod-200', 'name' =\u003e 'chair'],\n    ]);\n\n    $keyed = $collection-\u003ekeyBy('product_id');\n\n    $keyed-\u003eall();\n\n    /*\n        [\n            'prod-100' =\u003e ['product_id' =\u003e 'prod-100', 'name' =\u003e 'Desk'],\n            'prod-200' =\u003e ['product_id' =\u003e 'prod-200', 'name' =\u003e 'Chair'],\n        ]\n    */\n\nIf multiple items have the same key, only the last one will appear in the new collection.\n\nYou may also pass your own callback, which should return the value to key the collection by:\n\n    $keyed = $collection-\u003ekeyBy(function ($item) {\n        return strtoupper($item['product_id']);\n    });\n\n    $keyed-\u003eall();\n\n    /*\n        [\n            'PROD-100' =\u003e ['product_id' =\u003e 'prod-100', 'name' =\u003e 'Desk'],\n            'PROD-200' =\u003e ['product_id' =\u003e 'prod-200', 'name' =\u003e 'Chair'],\n        ]\n    */\n\n\n\u003ca name=\"method-keys\"\u003e\u003c/a\u003e\n#### `keys()`\n\nThe `keys` method returns all of the collection's keys:\n\n    $collection = collect([\n        'prod-100' =\u003e ['product_id' =\u003e 'prod-100', 'name' =\u003e 'Desk'],\n        'prod-200' =\u003e ['product_id' =\u003e 'prod-200', 'name' =\u003e 'Chair'],\n    ]);\n\n    $keys = $collection-\u003ekeys();\n\n    $keys-\u003eall();\n\n    // ['prod-100', 'prod-200']\n\n\u003ca name=\"method-last\"\u003e\u003c/a\u003e\n#### `last()`\n\nThe `last` method returns the last element in the collection that passes a given truth test:\n\n    collect([1, 2, 3, 4])-\u003elast(function ($key, $value) {\n        return $value \u003c 3;\n    });\n\n    // 2\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    collect([1, 2, 3, 4])-\u003elast();\n\n    // 4\n\n\u003ca name=\"method-map\"\u003e\u003c/a\u003e\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\n    $collection = collect([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\u003e **Note:** 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`](#method-transform) method.\n\n\u003ca name=\"method-max\"\u003e\u003c/a\u003e\n#### `max()`\n\nThe `max` method return the maximum value of a given key:\n\n    $max = collect([['foo' =\u003e 10], ['foo' =\u003e 20]])-\u003emax('foo');\n\n    // 20\n\n    $max = collect([1, 2, 3, 4, 5])-\u003emax();\n\n    // 5\n\n\u003ca name=\"method-merge\"\u003e\u003c/a\u003e\n#### `merge()`\n\nThe `merge` method merges the given array into the collection. Any string key in the array matching a string key in the collection will overwrite the value in the collection:\n\n    $collection = collect(['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\nIf the given array's keys are numeric, the values will be appended to the end of the collection:\n\n    $collection = collect(['Desk', 'Chair']);\n\n    $merged = $collection-\u003emerge(['Bookcase', 'Door']);\n\n    $merged-\u003eall();\n\n    // ['Desk', 'Chair', 'Bookcase', 'Door']\n\n\u003ca name=\"method-min\"\u003e\u003c/a\u003e\n#### `min()`\n\nThe `min` method return the minimum value of a given key:\n\n    $min = collect([['foo' =\u003e 10], ['foo' =\u003e 20]])-\u003emin('foo');\n\n    // 10\n\n    $min = collect([1, 2, 3, 4, 5])-\u003emin();\n\n    // 1\n\n\u003ca name=\"method-only\"\u003e\u003c/a\u003e\n#### `only()`\n\nThe `only` method returns the items in the collection with the specified keys:\n\n    $collection = collect(['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\nFor the inverse of `only`, see the [except](#method-except) method.\n\n\u003ca name=\"method-pluck\"\u003e\u003c/a\u003e\n#### `pluck()`\n\nThe `pluck` method retrieves all of the collection values for a given key:\n\n    $collection = collect([\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\nYou may also specify how you wish the resulting collection to be keyed:\n\n    $plucked = $collection-\u003epluck('name', 'product_id');\n\n    $plucked-\u003eall();\n\n    // ['prod-100' =\u003e 'Desk', 'prod-200' =\u003e 'Chair']\n\n\u003ca name=\"method-pop\"\u003e\u003c/a\u003e\n#### `pop()`\n\nThe `pop` method removes and returns the last item from the collection:\n\n    $collection = collect([1, 2, 3, 4, 5]);\n\n    $collection-\u003epop();\n\n    // 5\n\n    $collection-\u003eall();\n\n    // [1, 2, 3, 4]\n\n\u003ca name=\"method-prepend\"\u003e\u003c/a\u003e\n#### `prepend()`\n\nThe `prepend` method adds an item to the beginning of the collection:\n\n    $collection = collect([1, 2, 3, 4, 5]);\n\n    $collection-\u003eprepend(0);\n\n    $collection-\u003eall();\n\n    // [0, 1, 2, 3, 4, 5]\n\nYou can optionally pass a second argument to set the key of the prepended item:\n\n    $collection = collect(['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\u003ca name=\"method-pull\"\u003e\u003c/a\u003e\n#### `pull()`\n\nThe `pull` method removes and returns an item from the collection by its key:\n\n    $collection = collect(['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\u003ca name=\"method-push\"\u003e\u003c/a\u003e\n#### `push()`\n\nThe `push` method appends an item to the end of the collection:\n\n    $collection = collect([1, 2, 3, 4]);\n\n    $collection-\u003epush(5);\n\n    $collection-\u003eall();\n\n    // [1, 2, 3, 4, 5]\n\n\u003ca name=\"method-put\"\u003e\u003c/a\u003e\n#### `put()`\n\nThe `put` method sets the given key and value in the collection:\n\n    $collection = collect(['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\u003ca name=\"method-random\"\u003e\u003c/a\u003e\n#### `random()`\n\nThe `random` method returns a random item from the collection:\n\n    $collection = collect([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    $random = $collection-\u003erandom(3);\n\n    $random-\u003eall();\n\n    // [2, 4, 5] - (retrieved randomly)\n\n\u003ca name=\"method-reduce\"\u003e\u003c/a\u003e\n#### `reduce()`\n\nThe `reduce` method reduces the collection to a single value, passing the result of each iteration into the subsequent iteration:\n\n    $collection = collect([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    $collection-\u003ereduce(function ($carry, $item) {\n        return $carry + $item;\n    }, 4);\n\n    // 10\n\n\u003ca name=\"method-reject\"\u003e\u003c/a\u003e\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\n    $collection = collect([1, 2, 3, 4]);\n\n    $filtered = $collection-\u003ereject(function ($item) {\n        return $item \u003e 2;\n    });\n\n    $filtered-\u003eall();\n\n    // [1, 2]\n\nFor the inverse of the `reject` method, see the [`filter`](#method-filter) method.\n\n\u003ca name=\"method-reverse\"\u003e\u003c/a\u003e\n#### `reverse()`\n\nThe `reverse` method reverses the order of the collection's items:\n\n    $collection = collect([1, 2, 3, 4, 5]);\n\n    $reversed = $collection-\u003ereverse();\n\n    $reversed-\u003eall();\n\n    // [5, 4, 3, 2, 1]\n\n\u003ca name=\"method-search\"\u003e\u003c/a\u003e\n#### `search()`\n\nThe `search` method searches the collection for the given value and returns its key if found. If the item is not found, `false` is returned.\n\n    $collection = collect([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\n    $collection-\u003esearch('4', true);\n\n    // false\n\nAlternatively, you may pass in your own callback to search for the first item that passes your truth test:\n\n    $collection-\u003esearch(function ($item, $key) {\n        return $item \u003e 5;\n    });\n\n    // 2\n\n\u003ca name=\"method-shift\"\u003e\u003c/a\u003e\n#### `shift()`\n\nThe `shift` method removes and returns the first item from the collection:\n\n    $collection = collect([1, 2, 3, 4, 5]);\n\n    $collection-\u003eshift();\n\n    // 1\n\n    $collection-\u003eall();\n\n    // [2, 3, 4, 5]\n\n\u003ca name=\"method-shuffle\"\u003e\u003c/a\u003e\n#### `shuffle()`\n\nThe `shuffle` method randomly shuffles the items in the collection:\n\n    $collection = collect([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\u003ca name=\"method-slice\"\u003e\u003c/a\u003e\n#### `slice()`\n\nThe `slice` method returns a slice of the collection starting at the given index:\n\n    $collection = collect([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    $slice = $collection-\u003eslice(4, 2);\n\n    $slice-\u003eall();\n\n    // [5, 6]\n\nThe returned slice will have new, numerically indexed keys. If you wish to preserve the original keys, pass `true` as the third argument to the method.\n\n\u003ca name=\"method-sort\"\u003e\u003c/a\u003e\n#### `sort()`\n\nThe `sort` method sorts the collection:\n\n    $collection = collect([5, 3, 1, 2, 4]);\n\n    $sorted = $collection-\u003esort();\n\n    $sorted-\u003evalues()-\u003eall();\n\n    // [1, 2, 3, 4, 5]\n\nThe sorted collection keeps the original array keys. In this example we used the [`values`](#method-values) method to reset the keys to consecutively numbered indexes.\n\nFor sorting a collection of nested arrays or objects, see the [`sortBy`](#method-sortby) and [`sortByDesc`](#method-sortbydesc) methods.\n\nIf your sorting needs are more advanced, you may pass a callback to `sort` with your own algorithm. Refer to the PHP documentation on [`usort`](http://php.net/manual/en/function.usort.php#refsect1-function.usort-parameters), which is what the collection's `sort` method calls under the hood.\n\n\u003ca name=\"method-sortby\"\u003e\u003c/a\u003e\n#### `sortBy()`\n\nThe `sortBy` method sorts the collection by the given key:\n\n    $collection = collect([\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\nThe sorted collection keeps the original array keys. In this example we used the [`values`](#method-values) method to reset the keys to consecutively numbered indexes.\n\nYou can also pass your own callback to determine how to sort the collection values:\n\n    $collection = collect([\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\u003ca name=\"method-sortbydesc\"\u003e\u003c/a\u003e\n#### `sortByDesc()`\n\nThis method has the same signature as the [`sortBy`](#method-sortby) method, but will sort the collection in the opposite order.\n\n\u003ca name=\"method-splice\"\u003e\u003c/a\u003e\n#### `splice()`\n\nThe `splice` method removes and returns a slice of items starting at the specified index:\n\n    $collection = collect([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\n    $collection = collect([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\n    $collection = collect([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\u003ca name=\"method-sum\"\u003e\u003c/a\u003e\n#### `sum()`\n\nThe `sum` method returns the sum of all items in the collection:\n\n    collect([1, 2, 3, 4, 5])-\u003esum();\n\n    // 15\n\nIf the collection contains nested arrays or objects, you should pass a key to use for determining which values to sum:\n\n    $collection = collect([\n        ['name' =\u003e 'JavaScript: The Good Parts', 'pages' =\u003e 176],\n        ['name' =\u003e 'JavaScript: The Definitive Guide', 'pages' =\u003e 1096],\n    ]);\n\n    $collection-\u003esum('pages');\n\n    // 1272\n\nIn addition, you may pass your own callback to determine which values of the collection to sum:\n\n    $collection = collect([\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\u003ca name=\"method-take\"\u003e\u003c/a\u003e\n#### `take()`\n\nThe `take` method returns a new collection with the specified number of items:\n\n    $collection = collect([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\n    $collection = collect([0, 1, 2, 3, 4, 5]);\n\n    $chunk = $collection-\u003etake(-2);\n\n    $chunk-\u003eall();\n\n    // [4, 5]\n\n\u003ca name=\"method-toarray\"\u003e\u003c/a\u003e\n#### `toArray()`\n\nThe `toArray` method converts the collection into a plain PHP `array`:\n\n    $collection = collect(['name' =\u003e 'Desk', 'price' =\u003e 200]);\n\n    $collection-\u003etoArray();\n\n    /*\n        [\n            ['name' =\u003e 'Desk', 'price' =\u003e 200],\n        ]\n    */\n\n\u003e **Note:** `toArray` also converts all of its nested objects to an array. If you want to get the underlying array as is, use the [`all`](#method-all) method instead.\n\n\u003ca name=\"method-tojson\"\u003e\u003c/a\u003e\n#### `toJson()`\n\nThe `toJson` method converts the collection into JSON:\n\n    $collection = collect(['name' =\u003e 'Desk', 'price' =\u003e 200]);\n\n    $collection-\u003etoJson();\n\n    // '{\"name\":\"Desk\",\"price\":200}'\n\n\u003ca name=\"method-transform\"\u003e\u003c/a\u003e\n#### `transform()`\n\nThe `transform` method iterates over the collection and calls the given callback with each item in the collection. The items in the collection will be replaced by the values returned by the callback:\n\n    $collection = collect([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 **Note:** Unlike most other collection methods, `transform` modifies the collection itself. If you wish to create a new collection instead, use the [`map`](#method-map) method.\n\n\u003ca name=\"method-unique\"\u003e\u003c/a\u003e\n#### `unique()`\n\nThe `unique` method returns all of the unique items in the collection:\n\n    $collection = collect([1, 1, 2, 2, 3, 4, 2]);\n\n    $unique = $collection-\u003eunique();\n\n    $unique-\u003evalues()-\u003eall();\n\n    // [1, 2, 3, 4]\n\nThe returned collection keeps the original array keys. In this example we used the [`values`](#method-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    $collection = collect([\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    $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\u003ca name=\"method-values\"\u003e\u003c/a\u003e\n#### `values()`\n\nThe `values` method returns a new collection with the keys reset to consecutive integers:\n\n    $collection = collect([\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\u003ca name=\"method-where\"\u003e\u003c/a\u003e\n#### `where()`\n\nThe `where` method filters the collection by a given key / value pair:\n\n    $collection = collect([\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\nThe `where` method uses strict comparisons when checking item values. Use the [`whereLoose`](#where-loose) method to filter using \"loose\" comparisons.\n\n\u003ca name=\"method-whereloose\"\u003e\u003c/a\u003e\n#### `whereLoose()`\n\nThis method has the same signature as the [`where`](#method-where) method; however, all values are compared using \"loose\" comparisons.\n\n\u003ca name=\"method-zip\"\u003e\u003c/a\u003e\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    $collection = collect(['Chair', 'Desk']);\n\n    $zipped = $collection-\u003ezip([100, 200]);\n\n    $zipped-\u003eall();\n\n    // [['Chair', 100], ['Desk', 200]]\n\n\u003ca name=\"updates\"\u003e\u003c/a\u003e\n## When will updates be made?\n\nThis repository will be updated when significant/major changes have been made to Laravel's collection classes. Think it should be updated? Open an issue.\n\n\u003ca name=\"howtocontribute\"\u003e\u003c/a\u003e\n## Can I contribute improvements/fixes?\n\nThis repository is automagically generated using a script that involves a lot of black magic/sorcery. This script pulls the collection code from the Laravel repository and \"rewrites\" it so it can be used by itself. Because of this, any modifications made to the code in this repository would be overwritten whenever the script is ran. If you are still interested in contributing, consider contributing directly to [Laravel](https://github.com/laravel/framework).\n\n\u003ca name=\"motivation\"\u003e\u003c/a\u003e\n## Motivation/Why is this a thing?\n\nThe `Collection` helper class is *extremely* useful and it might as well be its own Composer package (because why not?). But mainly this was an experiment to see if I could create a little script to automatically generate the code seen in this repository from the code in the Laravel repository.\n\nHowever, because the `Collection` helper class is so useful, I am sure someone will find value in this little library.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstillat%2Fcollection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstillat%2Fcollection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstillat%2Fcollection/lists"}