PHPackages                             loophp/collection - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. loophp/collection

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

loophp/collection
=================

A (memory) friendly, easy, lazy and modular collection class.

7.6.1(1y ago)745663.8k—6.1%35[3 PRs](https://github.com/loophp/collection/pulls)12MITPHPPHP &gt;= 8.1CI passing

Since Aug 12Pushed 1mo ago11 watchersCompare

[ Source](https://github.com/loophp/collection)[ Packagist](https://packagist.org/packages/loophp/collection)[ Docs](https://github.com/loophp/collection)[ GitHub Sponsors](https://github.com/drupol)[ RSS](/packages/loophp-collection/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (12)Versions (139)Used By (12)

[![Try!](https://camo.githubusercontent.com/498a4f9011c97bd69c147a1bae4ad7813e7ce833127ad21c6d6e1b39e9f8ff23/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f54727925323069742d6f6e6c696e65253230212d627269676874677265656e3f7374796c653d666c61742d737175617265)](https://play.phpsandbox.io/loophp/collection)[![Latest Stable Version](https://camo.githubusercontent.com/56c5b69ce9547a375aafc13b26e2f16f2a4abbd55ba16bbabd982cd98f75b59e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c6f6f7068702f636f6c6c656374696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/loophp/collection)[![GitHub stars](https://camo.githubusercontent.com/61c9a2ce39d1f2e9ac98a2496897d2783d9e84f5d0f50db2e83f9950f3e1fe28/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6c6f6f7068702f636f6c6c656374696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/loophp/collection)[![Total Downloads](https://camo.githubusercontent.com/0e74fc1e770a1a33191256a77702b4ca5cf059d9d40150403c36d56570300865/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c6f6f7068702f636f6c6c656374696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/loophp/collection)[![GitHub Workflow Status](https://camo.githubusercontent.com/eded4d50ba923c9a12859eea423b30739d7c8a76750bfa79a4afb08abfe8c7d9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6c6f6f7068702f636f6c6c656374696f6e2f74657374732e796d6c3f6272616e63683d6d6173746572267374796c653d666c61742d737175617265)](https://github.com/loophp/collection/actions)[![Scrutinizer code quality](https://camo.githubusercontent.com/209a83a14883c46db3856020f0d126d729e53adc8a827c625f958904f4be43ed/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f7175616c6974792f672f6c6f6f7068702f636f6c6c656374696f6e2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/loophp/collection/?branch=master)[![Type Coverage](https://camo.githubusercontent.com/a2e0b47568a3b53c3ce95a1464d5b6ef90082d257aa4c58cbfe8b51c9e5799aa/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f64796e616d69632f6a736f6e3f7374796c653d666c61742d73717561726526636f6c6f723d636f6c6f72266c6162656c3d54797065253230636f7665726167652671756572793d6d6573736167652675726c3d687474707325334125324625324673686570686572642e6465762532466769746875622532466c6f6f706870253246636f6c6c656374696f6e253246636f766572616765)](https://shepherd.dev/github/loophp/collection)[![Code Coverage](https://camo.githubusercontent.com/ccf9ae16b95328d2943bde536f69374c0fd918c0b5e4196f01d0004c5c6a11e7/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6c6f6f7068702f636f6c6c656374696f6e2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/loophp/collection/?branch=master)[![Mutation testing badge](https://camo.githubusercontent.com/317ca10d9f46f77e952ecfb0cff63d6da75114789193c132806a77df102c72c1/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742d7371756172652675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d2532466c6f6f706870253246636f6c6c656374696f6e2532466d6173746572)](https://dashboard.stryker-mutator.io/reports/github.com/loophp/collection/master)[![License](https://camo.githubusercontent.com/1498dcfc7fd8908068c65a9b21b0fcd05e9f702330e8dafcd713f6685695b933/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c6f6f7068702f636f6c6c656374696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/loophp/collection)[![Donate!](https://camo.githubusercontent.com/a71f45de7e408be2477113d166e9ee94c90bbf814a1373fd154aa5b6652302de/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f53706f6e736f722d4769746875622d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/sponsors/drupol)

PHP Collection
==============

[](#php-collection)

Description
-----------

[](#description)

Collection is a functional utility library for PHP greater than 7.4, including PHP 8.0.

It's similar to [other collection libraries](https://packagist.org/?query=collection) based on regular PHP arrays, but with a lazy mechanism under the hood that strives to do as little work as possible while being as flexible as possible.

Functions like [`array_map()`](https://www.php.net/array-map), [`array_filter()`](https://www.php.net/array-filter) and [`array_reduce()`](https://www.php.net/array-reduce) are great, but they create new arrays and everything is eagerly done before going to the next step. Lazy collection leverages PHP's generators, iterators, and yield statements to allow you to work with very large data sets while keeping memory usage as low as possible.

For example, imagine your application needs to process a multi-gigabyte log file while taking advantage of this library's methods to parse the file. Instead of reading and storing the entire file into memory at once, this library may be used to keep only a small part of the file in memory at a given time.

On top of this, this library:

- is [immutable](https://en.wikipedia.org/wiki/Immutable_object),
- is extendable,
- extensively uses [S.O.L.I.D. principles](https://en.wikipedia.org/wiki/SOLID),
- leverages the power of PHP [generators](https://www.php.net/manual/en/class.generator.php) and [iterators](https://www.php.net/manual/en/class.iterator.php),
- extensively tested,
- uses strict types,
- framework agnostic.

Except for a few methods, most methods are [pure](https://en.wikipedia.org/wiki/Pure_function) and return a [new Collection object](https://github.com/loophp/collection/blob/master/src/Collection.php).

Also, unlike regular PHP arrays where keys must be either of type `int` or `string`, this collection library lets you use any kind of type for keys: `integer`, `string`, `object`, `array`, ... anything! This library could be a valid replacement for [\\SplObjectStorage](https://www.php.net/manual/en/class.splobjectstorage.php) but with much more features. This way of working opens up new perspectives and another way of handling data, in a more functional way.

And last but not least, collection keys are preserved throughout most operations; while it might lead to some confusion at first, please carefully read [this example](https://loophp-collection.readthedocs.io/en/stable/pages/usage.html#working-with-keys-and-values) for the full explanation and benefits.

This library has been inspired by:

- [Laravel Support Package](https://github.com/illuminate/support)
- [DusanKasan/Knapsack](https://github.com/DusanKasan/Knapsack)
- [mtdowling/transducers](https://github.com/mtdowling/transducers.php)
- [Ruby Array](https://ruby-doc.org/core-2.7.0/Array.html)
- [Collect.js](https://collect.js.org/)
- [nikic/iter](https://github.com/nikic/iter)
- [Haskell](https://www.haskell.org/)
- [Ramda](https://ramdajs.com/)
- [Lazy.js](http://danieltao.com/lazy.js/)

Features
--------

[](#features)

- **Decoupled**: Each Collection method is a shortcut to one isolated standard class, each operation has its own responsibility. Usually, the arguments needed are standard PHP variables like `int`, `string`, `callable` or `iterator`. It allows users to use those operations individually, at their own will, to build up something custom. Currently, more than [**100 operations**](https://loophp-collection.readthedocs.io/en/stable/pages/api.html) are available in this library. This library is an example of what you can do with all those small bricks, but nothing prevents users from using an operation on its own as well.
- **It takes function first, data-last**: In the following example, multiple operations are created. The data to be operated on is generally supplied at last.

    ```
