PHPackages                             bentools/iterable-functions - 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/iterable-functions

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

bentools/iterable-functions
===========================

Provides functions for iterable variables: is\_iterable(), iterable\_to\_array()

2.3(1y ago)23404.2k↓17.1%4[1 PRs](https://github.com/bpolaszek/php-iterable-functions/pulls)20MITPHPPHP ^8.1

Since Mar 10Pushed 1y ago2 watchersCompare

[ Source](https://github.com/bpolaszek/php-iterable-functions)[ Packagist](https://packagist.org/packages/bentools/iterable-functions)[ RSS](/packages/bentools-iterable-functions/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (19)Used By (20)

[![Latest Stable Version](https://camo.githubusercontent.com/40d7c9e97abe78b7dd570b44de075f5b6098f4121c40f73dd1a6e4b460a487fc/68747470733a2f2f706f7365722e707567782e6f72672f62656e746f6f6c732f6974657261626c652d66756e6374696f6e732f762f737461626c65)](https://packagist.org/packages/bentools/iterable-functions)[![GitHub Actions](https://github.com/bpolaszek/php-iterable-functions/workflows/Continuous%20Integration/badge.svg)](https://github.com/bpolaszek/php-iterable-functions/actions?query=workflow%3A%22Continuous+Integration%22+branch%3A2.0.x-dev)[![Code Coverage](https://camo.githubusercontent.com/127721f70e01fa9012d0984c00fbe585bee301e9e4e4815c28b8111b0ba27909/68747470733a2f2f636f6465636f762e696f2f67682f62706f6c61737a656b2f7068702d6974657261626c652d66756e6374696f6e732f6272616e63682f322e302e782d6465762f67726170682f62616467652e737667)](https://codecov.io/gh/bpolaszek/php-iterable-functions/branch/2.0.x-dev)[![Shepherd Type](https://camo.githubusercontent.com/a6b7be2f103411f7d32785cc5c771903d0a1fe72ca8ba71934c50344a947bf5d/68747470733a2f2f73686570686572642e6465762f6769746875622f62706f6c61737a656b2f7068702d6974657261626c652d66756e6374696f6e732f636f7665726167652e737667)](https://shepherd.dev/github/bpolaszek/php-iterable-functions)[![Total Downloads](https://camo.githubusercontent.com/656986dd7802774ad724e7558393956703e17c728f296e36caecee3e10c1739c/68747470733a2f2f706f7365722e707567782e6f72672f62656e746f6f6c732f6974657261626c652d66756e6374696f6e732f646f776e6c6f616473)](https://packagist.org/packages/bentools/iterable-functions)

Iterable functions
==================

[](#iterable-functions)

This package provides functions to work with [iterables](https://wiki.php.net/rfc/iterable), as you usually do with arrays:

- [iterable\_to\_array()](#iterable_to_array)
- [iterable\_to\_traversable()](#iterable_to_traversable)
- [iterable\_map()](#iterable_map)
- [iterable\_merge()](#iterable_merge)
- [iterable\_reduce()](#iterable_reduce)
- [iterable\_filter()](#iterable_filter)
- [iterable\_values()](#iterable_values)
- [iterable\_chunk()](#iterable_chunk)

iterable\_to\_array()
---------------------

[](#iterable_to_array)

PHP offers an `iterator_to_array()` function to export any iterator into an array.

**But when you want to transform an `iterable` to an array, the `iterable` itself can already be an array.**

When using `iterator_to_array()` with an iterable, that happens to be an array, PHP will throw a `TypeError`.

If you need an iterable-agnostic function, try our `iterable_to_array()`:

```
use function BenTools\IterableFunctions\iterable_to_array;

var_dump(iterable_to_array(new \ArrayIterator(['foo', 'bar']))); // ['foo', 'bar']
var_dump(iterable_to_array(['foo', 'bar'])); // ['foo', 'bar']
```

iterable\_to\_traversable()
---------------------------

[](#iterable_to_traversable)

Useful when you have a `Traversable` type-hint, and you don't know wether or not your argument will be an array or an iterator.

If your variable is already an instance of `Traversable` (i.e. an `Iterator`, an `IteratorAggregate` or a `Generator`), the function simply returns it directly.

If your variable is an array, the function converts it to an `ArrayIterator`.

Usage:

```
use function BenTools\IterableFunctions\iterable_to_traversable;

var_dump(iterable_to_traversable(['foo', 'bar'])); // \ArrayIterator(['foo', 'bar'])
var_dump(iterable_to_traversable(new \ArrayIterator(['foo', 'bar']))); // \ArrayIterator(['foo', 'bar'])
```

iterable\_map()
---------------

[](#iterable_map)

Works like an `array_map` with an `array` or a `Traversable`.

```
use function BenTools\IterableFunctions\iterable_map;

$generator = function () {
    yield 'foo';
    yield 'bar';
};

foreach (iterable_map($generator(), 'strtoupper') as $item) {
    var_dump($item); // FOO, BAR
}
```

iterable\_merge()
-----------------

[](#iterable_merge)

Works like an `array_merge` with an `array` or a `Traversable`.

```
use function BenTools\IterableFunctions\iterable_merge;

$generator1 = function () {
    yield 'foo';
};

$generator2 = function () {
    yield 'bar';
};

foreach (iterable_merge($generator1(), $generator2()) as $item) {
    var_dump($item); // foo, bar
}
```

iterable\_reduce()
------------------

[](#iterable_reduce)

Works like an `reduce` with an `iterable`.

```
use function BenTools\IterableFunctions\iterable_reduce;

$generator = function () {
    yield 1;
    yield 2;
};

$reduce = static function ($carry, $item) {
    return $carry + $item;
};

var_dump(
    iterable_reduce($generator(), $reduce, 0))
); // 3
```

iterable\_filter()
------------------

[](#iterable_filter)

Works like an `array_filter` with an `array` or a `Traversable`.

```
use function BenTools\IterableFunctions\iterable_filter;

$generator = function () {
    yield 0;
    yield 1;
};

foreach (iterable_filter($generator()) as $item) {
    var_dump($item); // 1
}
```

Of course you can define your own filter:

```
use function BenTools\IterableFunctions\iterable_filter;

$generator = function () {
    yield 'foo';
    yield 'bar';
};

$filter = function ($value) {
    return 'foo' !== $value;
};

foreach (iterable_filter($generator(), $filter) as $item) {
    var_dump($item); // bar
}
```

iterable\_values()
------------------

[](#iterable_values)

Works like an `array_values` with an `array` or a `Traversable`.

```
use function BenTools\IterableFunctions\iterable_values;

$generator = function () {
    yield 'a' => 'a';
    yield 'b' => 'b';
};

foreach (iterable_values($generator()) as $key => $value) {
    var_dump($key); // 0, 1
    var_dump($value); // a, b
}
```

iterable\_chunk()
-----------------

[](#iterable_chunk)

Here's an `array_chunk`-like function that also works with a `Traversable`.

```
use function BenTools\IterableFunctions\iterable_chunk;

$fruits = [
    'banana',
    'apple',
    'strawberry',
    'raspberry',
    'pineapple',
]
$fruits = (fn () => yield from $fruits)()
iterable_chunk($fruits, 2);

/*
  [
    ['banana', 'apple'],
    ['strawberry', 'raspberry'],
    ['pineapple'],
  ]
 */
```

Iterable fluent interface
=========================

[](#iterable-fluent-interface)

The `iterable` function allows you to wrap an iterable and apply some common operations.

With an array input:

```
use function BenTools\IterableFunctions\iterable;
$data = [
    'banana',
    'pineapple',
    'rock',
];

$iterable = iterable($data)->filter(fn($eatable) => 'rock' !== $eatable)->map('strtoupper'); // Traversable of ['banana', 'pineapple']
```

With a traversable input:

```
use function BenTools\IterableFunctions\iterable;
$data = [
    'banana',
    'pineapple',
    'rock',
];

$data = fn() => yield from $data;

$iterable = iterable($data())->filter(fn($eatable) => 'rock' !== $eatable)->map('strtoupper'); // Traversable of ['banana', 'pineapple']
```

Array output:

```
$iterable->asArray(); // array ['banana', 'pineapple']
```

Installation
============

[](#installation)

```
composer require bentools/iterable-functions:^2.0

```

For PHP5+ compatibility, check out the [1.x branch](https://github.com/bpolaszek/php-iterable-functions/tree/1.x).

Unit tests
==========

[](#unit-tests)

```
php vendor/bin/pest

```

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity45

Moderate usage in the ecosystem

Community27

Small or concentrated contributor base

Maturity80

Battle-tested with a long release history

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~165 days

Recently: every ~110 days

Total

17

Last Release

713d ago

Major Versions

1.x-dev → 2.0-beta2021-02-28

PHP version history (4 changes)1.0PHP &gt;=5.3

2.0-betaPHP ^7.3 || ^8.0

2.0PHP ^7.4 || ^8.0

2.2PHP ^8.1

### 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 (34 commits)")[![simPod](https://avatars.githubusercontent.com/u/327717?v=4)](https://github.com/simPod "simPod (31 commits)")[![ben-synapse](https://avatars.githubusercontent.com/u/36077477?v=4)](https://github.com/ben-synapse "ben-synapse (4 commits)")[![mwijngaard](https://avatars.githubusercontent.com/u/273701?v=4)](https://github.com/mwijngaard "mwijngaard (2 commits)")[![jdecool](https://avatars.githubusercontent.com/u/433926?v=4)](https://github.com/jdecool "jdecool (1 commits)")[![weph](https://avatars.githubusercontent.com/u/1123079?v=4)](https://github.com/weph "weph (1 commits)")

---

Tags

arrayiterableiteratoriteratoraggregatephptraversable

###  Code Quality

TestsPest

Static AnalysisPHPStan, Psalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/bentools-iterable-functions/health.svg)

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

###  Alternatives

[emil/inliner

1911.3k1](/packages/emil-inliner)[flowpack/nodegenerator

Random nodes generator for Neos CMS

122.1k](/packages/flowpack-nodegenerator)[haariga/craft-gonzo

Component Library from your templates Folder

121.7k](/packages/haariga-craft-gonzo)

PHPackages © 2026

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