PHPackages                             suramon/itertools - 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. suramon/itertools

ActiveLibrary

suramon/itertools
=================

Iterators implemented in PHP, copy-pasted from Python

1.6.5(7y ago)1819.1k6MITPHPPHP &gt;=5.3CI failing

Since Sep 26Pushed 5y ago4 watchersCompare

[ Source](https://github.com/SuRaMoN/itertools)[ Packagist](https://packagist.org/packages/suramon/itertools)[ Docs](https://github.com/SuRaMoN/itertools)[ RSS](/packages/suramon-itertools/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (1)Dependencies (2)Versions (56)Used By (0)

itertools
=========

[](#itertools)

A set of iterators for PHP based on pythons [itertools](https://docs.python.org/2/library/itertools.html).

[![Build Status](https://camo.githubusercontent.com/d3a033a9802723fd41583780044856c12f0009244e07c41fbbeba0faf4e4d5fb/68747470733a2f2f7472617669732d63692e6f72672f537552614d6f4e2f69746572746f6f6c732e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/SuRaMoN/itertools) [![Scrutinizer Quality Score](https://camo.githubusercontent.com/9d71e751886a47bce2ba49c0f94c0436169803691767d46fe5cac6b8b7bdd02d/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f537552614d6f4e2f69746572746f6f6c732f6261646765732f7175616c6974792d73636f72652e706e673f733d65356331323637356466316366653531396632653261386638393139376633336365623833303463)](https://scrutinizer-ci.com/g/SuRaMoN/itertools/) [![Code Coverage](https://camo.githubusercontent.com/e6a8cf581a22156caa962a29c6bb91cff3b0497b682ce4726efc0ff2c709ffe1/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f537552614d6f4e2f69746572746f6f6c732f6261646765732f636f7665726167652e706e673f733d35386634643264316365613866376135633465373632353430346166333834346562386632656262)](https://scrutinizer-ci.com/g/SuRaMoN/itertools/)

Some iterator examples
======================

[](#some-iterator-examples)

```
// iterate the lines of a csv file
$lines = new FileLineIterator('file.csv');

// filter all non unique lines
$uniqueLines = new UniqueIterator($lines);

// convert unique csv string lines to array
$rows = new StringCsvIterator($uniqueLines);

// extract column 1 from the csv file
$column1 = new MapIterator($rows, function($row) { return $row['column1']; });

// output all rows in parallel
foreach(new ForkingIterator($column1) as $row) {
    echo $row;
}
```

Install Guide
=============

[](#install-guide)

```
composer require suramon/itertools

```

Manual Install
--------------

[](#manual-install)

1. [Download itertools](https://github.com/SuRaMoN/itertools/archive/master.zip) directly from GitHub, or clone it with Git: `git clone https://github.com/SuRaMoN/relike.git`
2. Include the autoload header located in `src/autload.php` (eg: `require 'relike/src/autoload.php';`) or you can use [psr-0](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md)
3. Learn and use itertools by looking at the [examples](https://github.com/SuRaMoN/itertools), [tests](https://github.com/SuRaMoN/itertools/tree/master/tests) and [source code](https://github.com/SuRaMoN/itertools/tree/master/src/itertools)

Some iterators explained
========================

[](#some-iterators-explained)

ChainIterator
-------------

[](#chainiterator)

Iterator equivalent of flattening a 2-dimensional array.

```
$iterators = new ArrayIterator(array(new RangeIterator(1, 5), new RangeIterator(6, 10)));
$fattenedIterators = new ChainIterator($iterators); // will contain all numbers from 1 to 10
```

ChunkingIterator
----------------

[](#chunkingiterator)

Splits a iterator into smaller chunks. this can be used for batch processing.

```
$iterator = new RangeIterator();
$batchsize = 100;
foreach(new ChunkingIterator($iterator, $batchsize) as $chunk) {
    $pdo->starttransaction();
    foreach($chunk as $element) {
        // process the iterator elements. using the transaction inside the chunkiterator makes sure the transaction stays small
    }
    $pdo->commit();
}
```

ForkingIterator
---------------

[](#forkingiterator)

This linux-only iterator is designed to be iterated by a foreach loop and forks a new process for each iteration.

```
$elements = new RangeIterator(0, 10);
foreach(new ForkingIterator($elements) as $i) {
    var_dump($i, getmypid()); // wil spawn a new process to iterate each element
}
```

HistoryIterator
---------------

[](#historyiterator)

An iterator that keeps track of the elements it iterates. It differs from the CachingIterator in the standard PHP library because this implementations allows the history size to be specified.

```
$range = new HistoryIterator(new ArrayIterator(range(1, 10)));
foreach($range as $i) {
    if($range->hasPrev()) {
        echo $i, $range->prev(), "\n";
    }
}
```

MapIterator
-----------

[](#mapiterator)

Iterator equivalent or [array\_map](https://www.php.net/manual/en/function.array-map.php).

```
$positiveNumbers = new RangeIterator(0, INF); // all numbers from 0 to infinity
$positiveSquareNumbers = new MapIterator($positiveNumbers, function($n) {return $n*$n;}); // all positive square numbers
```

SliceIterator
-------------

[](#sliceiterator)

Iterator equivalent of [array\_slice](https://www.php.net/manual/en/function.array-slice.php).

```
$lines = new SliceIterator(new FileLineIterator('file.txt'), 0, 1000); // will iterate the first 1000 lines of the file

```

UniqueIterator
--------------

[](#uniqueiterator)

Iterator equivalent of [array\_unique](https://www.php.net/manual/en/function.array-unique.php) but only works for sorted input.

```
$uniqueEntries = new UniqueIterator(new ArrayIterator(array(1, 2, 2, 2, 3, 4, 2))); // will contain 1, 2, 3, 4, 2
```

RangeIterator
-------------

[](#rangeiterator)

Iterator equivalent of [range](https://www.php.net/manual/en/function.range.php).

```
$lines = new SliceIterator(new FileLineIterator('file.txt'), 0, 1000); // will iterate the first 1000 lines of the file
```

ZipIterator
-----------

[](#zipiterator)

Inspired by pythons [zip](https://docs.python.org/3.1/library/functions.html#zip) function. It can be constructed with an array of iterators and it iterates all of its arguments at the same index, returning during each iteration an array of the elements of each iterator on the same iteration positon

```
$csv1 = new FileCsvIterator('file1.csv');
$csv2 = new FileCsvIterator('file2.csv');
foreach(new ZipIterator(array($csv1, $csv2)) as $combinedRows) {
    $row1 = $combinedRows[0]; // a row in file1.csv
    $row2 = $combinedRows[1]; // row in file2.csv on same position
}
```

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 52.6% 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 ~38 days

Recently: every ~187 days

Total

52

Last Release

2661d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/852196?v=4)[SuRaMoN](/maintainers/SuRaMoN)[@SuRaMoN](https://github.com/SuRaMoN)

---

Top Contributors

[![SuRaMoN](https://avatars.githubusercontent.com/u/852196?v=4)](https://github.com/SuRaMoN "SuRaMoN (10 commits)")[![jcoppens](https://avatars.githubusercontent.com/u/3177095?v=4)](https://github.com/jcoppens "jcoppens (4 commits)")[![jorrit](https://avatars.githubusercontent.com/u/521449?v=4)](https://github.com/jorrit "jorrit (2 commits)")[![suysdavid](https://avatars.githubusercontent.com/u/13279363?v=4)](https://github.com/suysdavid "suysdavid (2 commits)")[![sanmai](https://avatars.githubusercontent.com/u/139488?v=4)](https://github.com/sanmai "sanmai (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/suramon-itertools/health.svg)

```
[![Health](https://phpackages.com/badges/suramon-itertools/health.svg)](https://phpackages.com/packages/suramon-itertools)
```

PHPackages © 2026

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