PHPackages                             bentools/flatten-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. bentools/flatten-iterator

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

bentools/flatten-iterator
=========================

Flattens Traversable or arrays into one iterator.

1.1(3y ago)32.0k1MITPHPPHP &gt;=7.4

Since Jul 17Pushed 3y ago1 watchersCompare

[ Source](https://github.com/bpolaszek/flatten-iterator)[ Packagist](https://packagist.org/packages/bentools/flatten-iterator)[ RSS](/packages/bentools-flatten-iterator/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (1)Dependencies (4)Versions (3)Used By (1)

[![Latest Stable Version](https://camo.githubusercontent.com/ffc2d932e2eabeb186a57d9eddce8d65b0b60ee37c954b9af918d62e75e4a5f3/68747470733a2f2f706f7365722e707567782e6f72672f62656e746f6f6c732f666c617474656e2d6974657261746f722f762f737461626c65)](https://packagist.org/packages/bentools/flatten-iterator)[![License](https://camo.githubusercontent.com/2d79b851c0e431a1ce1210a0399c6d5b3e1dac29d15c7b4c8e41d16a591a8ecf/68747470733a2f2f706f7365722e707567782e6f72672f62656e746f6f6c732f666c617474656e2d6974657261746f722f6c6963656e7365)](https://packagist.org/packages/bentools/flatten-iterator)[![CI Workflow](https://github.com/bpolaszek/flatten-iterator/actions/workflows/ci-workflow.yml/badge.svg)](https://github.com/bpolaszek/flatten-iterator/actions/workflows/ci-workflow.yml)[![codecov](https://camo.githubusercontent.com/5b76e13fd33fe0fb5ab8eba0bec13063b2dbe8b2ea937972428725cbb101dfbd/68747470733a2f2f636f6465636f762e696f2f67682f62706f6c61737a656b2f666c617474656e2d6974657261746f722f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d544736325a3656464a56)](https://codecov.io/gh/bpolaszek/flatten-iterator)[![Quality Score](https://camo.githubusercontent.com/593012dd20b9c61dd7fbac9e830d3fcd378b24a3d145d374f4f8a815070481fb/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f62706f6c61737a656b2f666c617474656e2d6974657261746f722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/bpolaszek/flatten-iterator)[![Total Downloads](https://camo.githubusercontent.com/0a30484b3ab408202b45d2c2454ddc0788bd98aa9cc075771af1bc066d8df632/68747470733a2f2f706f7365722e707567782e6f72672f62656e746f6f6c732f666c617474656e2d6974657261746f722f646f776e6c6f616473)](https://packagist.org/packages/bentools/flatten-iterator)

FlattenIterator
===============

[](#flatteniterator)

Flattens arrays and / or traversables. Accepts any `iterable` composed of `iterables`.

PHP 7.1+

Usage
-----

[](#usage)

```
use BenTools\FlattenIterator\FlattenIterator;

require_once __DIR__ . '/vendor/autoload.php';

$cities = [
    [
        'london' => 'London',
        'paris' => 'Paris',
    ],
    new \ArrayIterator([
        'berlin' => 'Berlin',
        'bruxelles' => 'Bruxelles',
    ]),
    (function () {
        yield 'budapest' => 'Budapest';
        yield 'prague' => 'Prague';
    })(),
];

foreach (new FlattenIterator($cities) as $city) {
    var_dump($city);
}
```

Output:

```
string(6) "London"
string(5) "Paris"
string(6) "Berlin"
string(9) "Bruxelles"
string(8) "Budapest"
string(6) "Prague"

```

Array output and fluent interface
---------------------------------

[](#array-output-and-fluent-interface)

You can use the built-in function to generate your flattened data, and export them as an array:

```
use function BenTools\FlattenIterator\flatten;
print_r(flatten($cities)->asArray());
```

Output:

```
array(6) {
  [0]=>
  string(6) "London"
  [1]=>
  string(5) "Paris"
  [2]=>
  string(6) "Berlin"
  [3]=>
  string(9) "Bruxelles"
  [4]=>
  string(8) "Budapest"
  [5]=>
  string(6) "Prague"
}
```

Preserve Keys
-------------

[](#preserve-keys)

Set `$preserveKeys` to `true` to preserve keys in your flattened data:

```
var_dump(flatten($cities, $preserveKeys = true)->asArray());
```

Output:

```
array(6) {
  ["london"]=>
  string(6) "London"
  ["paris"]=>
  string(5) "Paris"
  ["berlin"]=>
  string(6) "Berlin"
  ["bruxelles"]=>
  string(9) "Bruxelles"
  ["budapest"]=>
  string(8) "Budapest"
  ["prague"]=>
  string(6) "Prague"
}

```

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

[](#installation)

```
composer require bentools/flatten-iterator

```

Unit tests
----------

[](#unit-tests)

```
./vendor/bin/phpunit

```

See also
--------

[](#see-also)

[bentools/cartesian-product](https://github.com/bpolaszek/cartesian-product)

[bentools/string-combinations](https://github.com/bpolaszek/string-combinations)

[bentools/iterable-functions](https://github.com/bpolaszek/php-iterable-functions)

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1942 days

Total

2

Last Release

1282d ago

PHP version history (2 changes)1.0PHP &gt;=7.1

1.1PHP &gt;=7.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/acdd1a8ee0e657ddd06cf11f98a32100ef7121afb8aa270a5b295f5c29c038b3?d=identicon)[bpolaszek](/maintainers/bpolaszek)

---

Top Contributors

[![bpolaszek](https://avatars.githubusercontent.com/u/5569077?v=4)](https://github.com/bpolaszek "bpolaszek (2 commits)")

---

Tags

arraygeneratoriteratoriterableflattentraversablemultidimensionnal

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/bentools-flatten-iterator/health.svg)

```
[![Health](https://phpackages.com/badges/bentools-flatten-iterator/health.svg)](https://phpackages.com/packages/bentools-flatten-iterator)
```

###  Alternatives

[athari/yalinqo

YaLinqo, a LINQ-to-objects library for PHP

4561.2M5](/packages/athari-yalinqo)[nikic/iter

Iteration primitives using generators

1.1k5.9M38](/packages/nikic-iter)[ihor/nspl

Non-standard PHP library (NSPL) - functional primitives toolbox and more

381368.5k](/packages/ihor-nspl)[loophp/collection

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

745663.8k13](/packages/loophp-collection)[markrogoyski/itertools-php

Iteration tools for PHP

14911.3k](/packages/markrogoyski-itertools-php)[ginq/ginq

LINQ to Object inspired DSL for PHP

192257.5k3](/packages/ginq-ginq)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
