PHPackages                             tiny-blocks/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. tiny-blocks/collection

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

tiny-blocks/collection
======================

Models a type-safe, fluent collection API for PHP with eager and lazy pipelines over arrays, iterators, and generators.

2.5.0(3w ago)216.1k↓87.6%8MITPHPPHP ^8.5CI passing

Since Sep 27Pushed 3w ago1 watchersCompare

[ Source](https://github.com/tiny-blocks/collection)[ Packagist](https://packagist.org/packages/tiny-blocks/collection)[ Docs](https://github.com/tiny-blocks/collection)[ RSS](/packages/tiny-blocks-collection/feed)WikiDiscussions main Synced today

READMEChangelog (10)Dependencies (27)Versions (34)Used By (8)

Collection
==========

[](#collection)

[![License](https://camo.githubusercontent.com/f8df3091bbe1149f398a5369b2c39e896766f9f6efba3477c63e9b4aa940ef14/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e)](https://github.com/tiny-blocks/collection/blob/main/LICENSE)

- [Overview](#overview)
- [Installation](#installation)
- [How to use](#how-to-use)
    - [Concrete implementation](#concrete-implementation)
    - [Extending Collection](#extending-collection)
    - [Mapping to and from a source](#mapping-to-and-from-a-source)
    - [Writing](#writing)
        - [Adding elements](#adding-elements)
        - [Merging collections](#merging-collections)
        - [Removing elements](#removing-elements)
    - [Filtering](#filtering)
        - [Filter by predicate](#filter-by-predicate)
    - [Ordering](#ordering)
        - [Sort by order and custom comparator](#sort-by-order-and-custom-comparator)
    - [Retrieving](#retrieving)
        - [Retrieve count](#retrieve-count)
        - [Check if empty](#check-if-empty)
        - [Retrieve by condition](#retrieve-by-condition)
        - [Retrieve single elements](#retrieve-single-elements)
        - [Retrieve collection segments](#retrieve-collection-segments)
    - [Comparing](#comparing)
        - [Check if collection contains element](#check-if-collection-contains-element)
        - [Compare collections for equality](#compare-collections-for-equality)
    - [Aggregation](#aggregation)
    - [Transforming](#transforming)
        - [Applying actions without modifying elements](#applying-actions-without-modifying-elements)
        - [Grouping elements](#grouping-elements)
        - [Mapping elements](#mapping-elements)
        - [Flattening elements](#flattening-elements)
        - [Convert to array](#convert-to-array)
        - [Convert to JSON](#convert-to-json)
- [FAQ](#faq)
- [License](#license)
- [Contributing](#contributing)

Overview
--------

[](#overview)

Models a type-safe, fluent collection API for PHP, unifying arrays, iterators, and [generators](https://www.php.net/manual/en/language.generators.overview.php) behind a single Collectible contract. Supports both eager and lazy evaluation pipelines, with chainable operations for mapping, filtering, grouping, reducing, and joining. Designed for predictable memory and CPU profiles in data-intensive workloads.

Installation
------------

[](#installation)

```
composer require tiny-blocks/collection

```

How to use
----------

[](#how-to-use)

The library exposes the available behaviors through the `Collectible` interface and provides utilities to manipulate collections of various types.

### Concrete implementation

[](#concrete-implementation)

The `Collection` class implements the `Collectible` interface and provides a concrete implementation for handling collections.

It allows for adding, removing, filtering, and sorting elements, as well as transforming them into different formats like arrays and JSON.

The class is designed to work with generic key-value pairs, ensuring type safety and flexibility for a variety of use cases.

```
