PHPackages                             val/one-to-many-iterator - 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. val/one-to-many-iterator

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

val/one-to-many-iterator
========================

Helper iterator and generator for one-to-many joins

v1.0.1(11y ago)124UnlicensePHP

Since Aug 18Pushed 11y ago2 watchersCompare

[ Source](https://github.com/valeriangalliat/php-one-to-many-iterator)[ Packagist](https://packagist.org/packages/val/one-to-many-iterator)[ Docs](https://github.com/valeriangalliat/php-one-to-many-iterator)[ RSS](/packages/val-one-to-many-iterator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

One-to-many iterator
====================

[](#one-to-many-iterator)

> Helper iterator and generator for one-to-many joins.

Overview
--------

[](#overview)

When you want to fetch a one-to-many relation, you're probably using a `JOIN` to avoid the [N+1 selects problem](http://use-the-index-luke.com/sql/join/nested-loops-join-n1-problem).

Though, it may be difficult to iterate over the result, especially when you need the whole "many" part of the relation loaded for a process.

This is why I created this tiny library. It takes a `Traversable`of arrays, having a common key to distinguish the "one" part of the one-to-many relation, and sorted on this key (so it can yield items in streaming, without loading the whole set in memory).

It will then aggregate the "many" part of the relation in a configurable key.

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

[](#installation)

```
composer require val/one-to-many-iterator
```

Examples
--------

[](#examples)

Your database result iterator, once converted into an array, looks like this (a typical `JOIN`):

```
