PHPackages                             drupol/phpermutations - 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. drupol/phpermutations

ActiveLibrary

drupol/phpermutations
=====================

Generators and iterators, permutations and combinations.

1.4.0(6y ago)83706.3k—8.3%17[1 PRs](https://github.com/drupol/phpermutations/pulls)5MITPHPPHP &gt;= 7.1.3CI failing

Since Jan 6Pushed 1y ago2 watchersCompare

[ Source](https://github.com/drupol/phpermutations)[ Packagist](https://packagist.org/packages/drupol/phpermutations)[ Docs](https://github.com/drupol/phpermutations)[ GitHub Sponsors](https://github.com/drupol)[ RSS](/packages/drupol-phpermutations/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (3)Versions (15)Used By (5)

[![Latest Stable Version](https://camo.githubusercontent.com/3ba16b1cd0e24ee5a200688ad0674083220e8d9090fabc5679d615562d059e71/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f647275706f6c2f70687065726d75746174696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/drupol/phpermutations)[![GitHub stars](https://camo.githubusercontent.com/ef25a5e8155b9ba876768a8bf2bd6a5c7c3c6aab3aa346df3282c637cef0e999/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f647275706f6c2f70687065726d75746174696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/drupol/phpermutations)[![Total Downloads](https://camo.githubusercontent.com/8f8c0ef1d64b1b8e93c893b38ef5b497ee6c4a3bfd2a6a5453693b22403c92d6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f647275706f6c2f70687065726d75746174696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/drupol/phpermutations)[![GitHub Workflow Status](https://camo.githubusercontent.com/7cff8d861582bbb34654783076fc075c4f03c18e8c91dec12a9081d8d9431c90/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f647275706f6c2f70687065726d75746174696f6e732f436f6e74696e756f7573253230496e746567726174696f6e3f7374796c653d666c61742d737175617265)](https://github.com/drupol/phpermutations/actions)[![Scrutinizer code quality](https://camo.githubusercontent.com/430e43725426c08bfa9736c2f4735cfdb6c0b7ba740ee2568f0452214eb6476f/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f7175616c6974792f672f647275706f6c2f70687065726d75746174696f6e732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/drupol/phpermutations/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/751bb097fd21ecbb936753c75f77a1fb6e8393cb1bae3ef429fce86502c47524/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f647275706f6c2f70687065726d75746174696f6e732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/drupol/phpermutations/?branch=master)[![License](https://camo.githubusercontent.com/58489201128e61186bc4a7978d3744064727da4d4f955af2df8dca0416dd568a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f647275706f6c2f70687065726d75746174696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/drupol/phpermutations)[![Donate!](https://camo.githubusercontent.com/f2e288c194c16198351c80fcb396a39afb2ae2ccf89c1549c8dc049901151137/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f446f6e6174652d50617970616c2d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://paypal.me/drupol)

PHPermutations
--------------

[](#phpermutations)

PHP Iterators and Generators to generate combinations and permutations in an efficient way.

At first the library was created to only generate permutations and combinations.

In the end, I added other Iterators and Generators like:

- Fibonacci numbers,
- Perfect numbers,
- Prime numbers,
- Product of numbers,
- Rotation of an array,
- Cycling through an array,
- Permutations,
- Combinations,

Introduction
------------

[](#introduction)

I've always been fascinated by numbers and everything around them... in other words, mathematics.

The library has been written first for being used in [PHPartition](https://github.com/drupol/phpartition), then it has been extended here and there.

Its main use is for generating Permutations and Combinations without running out of memory, thanks to [PHP Generators](https://secure.php.net/manual/en/language.generators.overview.php) and and [Iterators](https://secure.php.net/manual/en/class.iterator.php).

The difference with other combinatorics library is that you can use an extra parameter 'length', that allows you to compute Permutations and Combinations of a particular size. The other notable difference is that your input arrays may contains any type of object (integers, arrays, strings or objects), the library will still continue to work without any trouble.

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

[](#requirements)

- PHP &gt;= 7.1.3,

How to use
----------

[](#how-to-use)

Include this library in your project by doing:

`composer require drupol/phpermutations`

Let's say you want to find all the permutations of the list of number \[1, 2, 3, 4, 5\] having a length of 3:

```
// Create the object
$permutations = new \drupol\phpermutations\Generators\Permutations([1,2,3,4,5], 3);

// Use a foreach loop.
foreach ($permutations->generator() as $permutation) {// do stuff}

// Or get the whole array at once.
$permutations->toArray();
```

Most of the components always has the same arguments except for very few of them.

As the documentation per component is not written yet, I advise you to [check the tests](https://github.com/drupol/phpermutations/tree/master/tests/src) to see how to use them.

Combinations
------------

[](#combinations)

> In mathematics, a combination is a way of selecting items from a collection, such that (unlike permutations) the order of selection does not matter. -- [*Wikipedia*](https://en.wikipedia.org/wiki/Combination)

In one sentence: *When the order doesn't matter, it is a Combination.*

Examples
--------

[](#examples)

Let's say we have a group of fruits:

`$list = ['Apple', 'Pear', 'Banana', 'Orange']`

and we want to find the combinations of length: `3`, the result will be:

`['Apple', 'Pear', 'Banana']`

`['Apple', 'Pear', 'Orange']`

`['Apple', 'Banana', 'Orange']`

`['Pear', 'Banana', 'Orange']`

Permutations
------------

[](#permutations)

> In mathematics, the notion of permutation relates to the act of arranging all the members of a set into some sequence or order, or if the set is already ordered, rearranging (reordering) its elements, a process called permuting. These differ from combinations, which are selections of some members of a set where order is disregarded. -- [*Wikipedia*](https://en.wikipedia.org/wiki/Permutation)

In one sentence: *When the order does matter, it is a Permutation.*

### Examples

[](#examples-1)

Let's say we have a group of fruits

`['Apple', 'Pear', 'Banana', 'Orange']`

and we want to find the permutations of length: `3`, the result will be:

`['Apple', 'Pear', 'Banana']`

`['Pear', 'Apple', 'Banana']`

`['Apple', 'Banana', 'Pear']`

`['Banana', 'Apple', 'Pear']`

`['Pear', 'Banana', 'Apple']`

`['Banana', 'Pear', 'Apple']`

`['Apple', 'Pear', 'Orange']`

`['Pear', 'Apple', 'Orange']`

`['Apple', 'Orange', 'Pear']`

`['Orange', 'Apple', 'Pear']`

`['Pear', 'Orange', 'Apple']`

`['Orange', 'Pear', 'Apple']`

`['Apple', 'Banana', 'Orange']`

`['Banana', 'Apple', 'Orange']`

`['Apple', 'Orange', 'Banana']`

`['Orange', 'Apple', 'Banana']`

`['Banana', 'Orange', 'Apple']`

`['Orange', 'Banana', 'Apple']`

`['Pear', 'Banana', 'Orange']`

`['Banana', 'Pear', 'Orange']`

`['Pear', 'Orange', 'Banana']`

`['Orange', 'Pear', 'Banana']`

`['Banana', 'Orange', 'Pear']`

`['Orange', 'Banana', 'Pear']`

The permutations of length 3 of the array \[1, 2, 3, 4, 5\] are, *please hold on tight*, the sum of all the permutations of each combinations having a length of 3 of that array.

Tests
-----

[](#tests)

Each Generators and Iterators are tested using the same values as input. I try to be as much complete as possible with the [tests](https://github.com/drupol/phpermutations/tree/master/tests/fixtures). Every time the sources are modified, [Github](https://github.com/drupol/phpermutations/actions), the continuous integration service, tests the code against those tests, this way you are aware if the changes that you are introducing are valid.

Contributing
============

[](#contributing)

Feel free to contribute to this library by sending Github pull requests. I'm quite reactive :-)

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity51

Moderate usage in the ecosystem

Community22

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 91.7% 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 ~110 days

Recently: every ~206 days

Total

12

Last Release

2199d ago

PHP version history (2 changes)1.3.0PHP &gt;= 5.6

1.4.0PHP &gt;= 7.1.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/252042?v=4)[Pol Dellaiera](/maintainers/drupol)[@drupol](https://github.com/drupol)

---

Top Contributors

[![drupol](https://avatars.githubusercontent.com/u/252042?v=4)](https://github.com/drupol "drupol (33 commits)")[![yemilgr](https://avatars.githubusercontent.com/u/915196?v=4)](https://github.com/yemilgr "yemilgr (2 commits)")[![DomBra27](https://avatars.githubusercontent.com/u/54847829?v=4)](https://github.com/DomBra27 "DomBra27 (1 commits)")

---

Tags

combinationsfibonaccigeneratormathematicspermutationphp-iteratorprime-numbersmathiteratorsproductnumbersfibonaccicombinationspermutationsprime numbers

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/drupol-phpermutations/health.svg)

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

###  Alternatives

[doctrine/collections

PHP Doctrine Collections library that adds additional functionality on top of PHP arrays.

6.0k411.1M1.2k](/packages/doctrine-collections)[brick/math

Arbitrary-precision arithmetic library

2.1k504.0M277](/packages/brick-math)[samsara/fermat

A library providing math and statistics operations for numbers of arbitrary size.

653.1k3](/packages/samsara-fermat)[akeneo-labs/pim-enhanced-connector

Provides PIM product reader with more option (choice on completeness, choice on enabled, delta based on last export time)

2135.6k](/packages/akeneo-labs-pim-enhanced-connector)

PHPackages © 2026

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