PHPackages                             pcrov/iteratorstackiterator - 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. pcrov/iteratorstackiterator

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

pcrov/iteratorstackiterator
===========================

Iterates over a stack of iterators, discarding them as they complete.

1.0.2(9y ago)215.1kMITPHPPHP ^7.0

Since Feb 18Pushed 7y ago1 watchersCompare

[ Source](https://github.com/pcrov/IteratorStackIterator)[ Packagist](https://packagist.org/packages/pcrov/iteratorstackiterator)[ Docs](https://github.com/pcrov/iteratorstackiterator)[ RSS](/packages/pcrov-iteratorstackiterator/feed)WikiDiscussions master Synced today

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

IteratorStackIterator
=====================

[](#iteratorstackiterator)

[![Build Status](https://camo.githubusercontent.com/72cf35dff0bdb591ee90b181d00cd2926703e815fb8b880b9f588345b5f4f489/68747470733a2f2f7472617669732d63692e6f72672f7063726f762f4974657261746f72537461636b4974657261746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/pcrov/IteratorStackIterator)[![Coverage Status](https://camo.githubusercontent.com/fa4f3741a11a43a5645af5fa234260e7409405096e8b80acbafc8bdac13fcf5b/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f7063726f762f4974657261746f72537461636b4974657261746f722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/pcrov/IteratorStackIterator?branch=master)[![License](https://camo.githubusercontent.com/d43095661baf3d504af7c91292d4260b13464a9da5d881235caf02d9c2e27972/68747470733a2f2f706f7365722e707567782e6f72672f7063726f762f6974657261746f72737461636b6974657261746f722f6c6963656e7365)](https://github.com/pcrov/IteratorStackIterator/blob/master/LICENSE)[![Latest Stable Version](https://camo.githubusercontent.com/d5fec856caeca77f60ed23592725df788cdbe25800173705f0f799e0fb281794/68747470733a2f2f706f7365722e707567782e6f72672f7063726f762f6974657261746f72737461636b6974657261746f722f762f737461626c65)](https://packagist.org/packages/pcrov/iteratorstackiterator)

Iterates over a stack of iterators, discarding them as they complete.

Requirements
------------

[](#requirements)

PHP 7 with either the [Ds extension](http://php.net/ds) or its [polyfill](https://github.com/php-ds/polyfill). The php-ds polyfill will be installed automatically when IteratorStackIterator is installed via composer and only loaded if the Ds extension is not installed and enabled.

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

[](#installation)

To install with composer:

```
composer require pcrov/iteratorstackiterator
```

Usage
-----

[](#usage)

This iterator implements [`OuterIterator`](http://php.net/outeriterator), adding `push()` and `pop()` methods to add and remove inner iterators, respectively.

`push()` will return the new size of the stack. `pop()` will return the iterator popped from the top.

The values returned from `key()` and `current()` will always be from the current position of the top iterator on the stack.

`next()` moves the cursor of the iterator at the top of the stack. If that iterator is no longer valid it is removed, as are any others that have completed until a valid iterator is found or the stack is empty.

`rewind()` will rewind all iterators left on the stack.

### Example 1

[](#example-1)

```
$stack = new \pcrov\IteratorStackIterator();
$stack->push(
    new ArrayIterator([1, 2, 3]),
    new ArrayIterator([4, 5, 6]),
    new ArrayIterator([7, 8, 9])
);

foreach ($stack as $value) {
    echo $value;
}

// output: 789456123
```

---

Iterators can be added to the stack after iteration has already begun. They will *not* automatically be rewound when added, so you should call `rewind()` on them prior if needed.

### Example 2

[](#example-2)

```
$stack = new \pcrov\IteratorStackIterator();
$stack->push(
    new ArrayIterator([1, 2, 3]),
    new ArrayIterator([4, 5, 6])
);
$stack->rewind();

while ($stack->valid()) {
    $value = $stack->current();
    echo $value;
    $stack->next();

    if ($value === 2) {
        $stack->push(new ArrayIterator([7, 8, 9]));
    }
}

// output: 456127893
```

Note that `next()` is called prior to pushing a new iterator, otherwise when the stack ran back down that position would be repeated (which in this case result would result in an infinite loop.)

---

If an inner iterator's cursor position is manipulated from outside the stack iterator the resulting behavior is undefined.

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity61

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

Total

4

Last Release

3575d ago

Major Versions

0.0.0 → 1.0.02016-02-24

### Community

Maintainers

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

---

Top Contributors

[![pcrov](https://avatars.githubusercontent.com/u/8586747?v=4)](https://github.com/pcrov "pcrov (17 commits)")

---

Tags

iteratorphpiteratorstack

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/pcrov-iteratorstackiterator/health.svg)

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

###  Alternatives

[nikic/iter

Iteration primitives using generators

1.1k6.4M54](/packages/nikic-iter)[loophp/collection

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

745762.4k16](/packages/loophp-collection)[athari/yalinqo

YaLinqo, a LINQ-to-objects library for PHP

4581.2M5](/packages/athari-yalinqo)[ihor/nspl

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

375369.6k](/packages/ihor-nspl)[ginq/ginq

LINQ to Object inspired DSL for PHP

192258.5k3](/packages/ginq-ginq)[chdemko/sorted-collections

Sorted Collections for PHP &gt;= 8.4

222.7M3](/packages/chdemko-sorted-collections)

PHPackages © 2026

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