PHPackages                             wrossmann/pc-iters - 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. wrossmann/pc-iters

ActiveLibrary

wrossmann/pc-iters
==================

Memory-efficient permutation and combination generators.

1.0.0(8y ago)021MITPHP

Since Jan 20Pushed 8y ago1 watchersCompare

[ Source](https://github.com/wrossmann/pc-iters)[ Packagist](https://packagist.org/packages/wrossmann/pc-iters)[ RSS](/packages/wrossmann-pc-iters/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

wrossmann/pc-iters
==================

[](#wrossmannpc-iters)

Memory-efficient PHP permutation and combination generators.

Both classes are generators that should incur no additional memory requirements beyond roughly one extra copy of the input set.

This library was written in response to a question regarding permuatation/combination generation in ##php on FreeNode, and published after a brief survey of alternatives showed only solutions that recursively generated incresasingly larger sets of results in-memory.

I would also like to include the word 'combinatorics' in this document so that search engines find it. :)

Caveats
-------

[](#caveats)

- The input array to the interators *must* be numerically-indexed with no gaps in the indexes. \[see: `array_values()`\]
- Neither generator makes allowances for gaps or repetitions, all elements are assumed to be unique.
- If you store the results of these generators you're still going to have memory issues.
- This is not an ideal method to calculate the number of possible permutations and combinations. \[see: Math\]
- This is not an ideal method to generate something like "all possible non-sequential, non-repeating IDs" \[see: Linear-Feedback Shift Registers or just use a UUID\]

Classes
-------

[](#classes)

- **CombinationIterator:** Generates all possible combinations of N elements from the input set.
    - Uses a recursive, Gray Code-ish method.
    - Stack depth should never be deeper than the number of element in the output set.
- **PermutationIterator:** Generates all possible arrangements of the input set.
    - Uses the non-recursive form of Heap's Algorithm.
    - Operates in-place on a copy of the input set.

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

[](#installation)

```
composer require wrossmann/pc-iters

```

Usage
-----

[](#usage)

### CombinationIterator

[](#combinationiterator)

Function signature and docblock:

```
/**
 * Given a set of items, generate all unique combinations of the
 * specified number of items using a Gray Code-ish method.
 *
 * @param	array	$set	The set of items
 * @param	int		$count	The number of items in the output set
 * @param	int		$begin	Offset in the set to start.
 * @param	int		$end	Offset in the set to end. [non-inclusive]
 */
public static function iterate($set, $count, $begin=NULL, $end=NULL)

```

Example:

```
use wrossmann\PCIters\CombinationIterator;

foreach( CombinationIterator::iterate([1,2,3,4,5], 3) as $comb ) {
	printf("%s\n", json_encode($comb));
}

```

Output:

```
[1,2,3]
[1,2,4]
[1,2,5]
[1,3,4]
[1,3,5]
[1,4,5]
[2,3,4]
[2,3,5]
[2,4,5]
[3,4,5]

```

### PermutationIterator

[](#permutationiterator)

Function signature and docblock:

```
/**
 * Given a set of items generate all possible unique arrangements
 * of items. Uses Heap's Algorithm.
 *
 * @param	array	$set	The set of items on which to operate.
 */
public static function iterate($set)

```

Example:

```
use wrossmann\PCIters\PermutationIterator;

foreach( PermutationIterator::iterate([1,2,3]) as $comb ) {
	printf("%s\n", json_encode($comb));
}

```

Output:

```
[1,2,3]
[2,1,3]
[3,1,2]
[1,3,2]
[2,3,1]
[3,2,1]

```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity63

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

Unknown

Total

1

Last Release

3037d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/07eeb654c6e5293a620e4eb42138f331bc966fe0c26e54b7581aee28ee5c03c7?d=identicon)[wrossmann](/maintainers/wrossmann)

---

Top Contributors

[![wrossmann](https://avatars.githubusercontent.com/u/4967671?v=4)](https://github.com/wrossmann "wrossmann (1 commits)")

### Embed Badge

![Health badge](/badges/wrossmann-pc-iters/health.svg)

```
[![Health](https://phpackages.com/badges/wrossmann-pc-iters/health.svg)](https://phpackages.com/packages/wrossmann-pc-iters)
```

PHPackages © 2026

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