PHPackages                             macocci7/php-combination - 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. macocci7/php-combination

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

macocci7/php-combination
========================

A simple PHP library to make combinations from array elements.

1.1.2(5mo ago)214.2k↓37.9%12MITPHPPHP &gt;=8.1

Since Nov 11Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/macocci7/PHP-Combination)[ Packagist](https://packagist.org/packages/macocci7/php-combination)[ RSS](/packages/macocci7-php-combination/feed)WikiDiscussions main Synced 1mo ago

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

PHP-Combination
===============

[](#php-combination)

1. Features
-----------

[](#1-features)

`PHP-Combination` is a simple PHP library to make combinations from array elements.

`PHP-Combination` can:

- create `all` combinations
- `sort` combinations
- create `pairs`
- create all combinations `of N` elements
- create all combinations `of A 2 B` elements
- create all combinations between multiple arrays
- be used in data provider of PHPUnit

2. Contents
-----------

[](#2-contents)

- [1. Features](#1-features)
- 2. Contents
- [3. Requirements](#3-requirements)
- [4. Installation](#4-installation)
- [5. Classes](#5-classes)
    - [5.1. Combination](#51-combination)
    - [5.2. CombinationGenerator](#52-combinationgenerator)
    - [5.3. How to choose the class to use](#53-how-to-choose-the-class-to-use)
- [6. Methods](#6-methods)
- [7. Limit on the Number of Array Elements](#7-limit-on-the-number-of-array-elements)
- [8. Usage](#8-usage)
    - [8.1. Basic Usage](#81-basic-usage)
        - [8.1.1. Combination](#811-combination)
        - [8.1.2. CombinationGenerator](#812-combinationgenerator)
    - [8.2. Using Combination](#82-using-combination)
    - [8.3. Using Combination with Sorting](#83-using-combination-with-sorting)
    - [8.4. Using CombinationGenerator](#84-using-combinationgenerator)
    - [8.5. Using In PHPUnit](#85-using-in-phpunit)
- [9. Examples](#9-examples)
- [10. LICENSE](#10-license)

3. Requirements
---------------

[](#3-requirements)

- PHP 8.1 or later
- Composer

4. Installation
---------------

[](#4-installation)

```
composer require macocci7/php-combination
```

5. Classes
----------

[](#5-classes)

There're 2 types of classes for the same methods.

### 5.1. Combination

[](#51-combination)

```
Macocci7\PhpCombination\Combination
```

This class returns the result as type of `array`.

### 5.2. CombinationGenerator

[](#52-combinationgenerator)

```
Macocci7\PhpCombination\CombinationGenerator
```

This class returns the result as type of `Generator` object.

### 5.3. How to choose the class to use

[](#53-how-to-choose-the-class-to-use)

There might be 3 factors.

1. **The number of elements of the param and result of all()**

    If the array of the param has $n elements,

    the number of array elements of returned array will be:

    ```
    2 ** $n - 1
    ```

    $nformulaelements12 \*\* 1 - 1152 \*\* 5 - 131102 \*\* 10 - 11,023202 \*\* 20 - 11,048,575302 \*\* 30 - 11,073,741,823402 \*\* 40 - 11,099,511,627,775502 \*\* 50 - 11,125,899,906,842,623602 \*\* 60 - 11,152,921,504,606,846,975622 \*\* 62 - 14,611,686,018,427,387,903
2. **Memory limit on your environment**

    Memory usage depends on:

    - the number of array elements to return
    - the type of data

    `Combination` uses much more memory than `CombinationGenerator`.
3. **execution time**

    Looping Generator takes much longer time than looping array.

In some cases, `CombinationGenerator` takes several times longer than `Combination`.

However, using an array with many elements as an argument can cause `Combination` to exceed memory limits.

In that case, use `CombinationGenerator`.

It will never exceeds the memory limit, and certainly complete the task.

6. Methods
----------

[](#6-methods)

### 6.1. Macocci7\\PhpCombination\\Combination

[](#61-macocci7phpcombinationcombination)

- `all()`: returns all combinations of the param
- `pairs()`: returns all pairs of the param
- `ofN()`: returns all combinations of N elements of the param
- `ofA2B()`: returns all combinations of A to B elements of the param
- `fromArrays()`: returns all combinations of multiple arrays as an instance of `Iterator`.

### 6.2. Macocci7\\PhpCombination\\CombinationGenenrator

[](#62-macocci7phpcombinationcombinationgenenrator)

- `all()`: returns all combinations of the param
- `pairs()`: returns all pairs of the param
- `ofN()`: returns all combinations of N elements of the param
- `ofA2B()`: returns all combinations of A to B elements of the param
- `fromArrays()`: returns all combinations of multiple arrays as an instance of `Iterator`.

7. Limit on the Number of Array Elements
----------------------------------------

[](#7-limit-on-the-number-of-array-elements)

The number of array elements of the param:

- 32bit-system: 30 elements
- 64bit-system: 62 elements

This limit is set to ensure that the number of elements in the returned array does not exceed the PHP upper limit on the index number of array.

The max index number of array in PHP equals to `PHP_INT_MAX`.

`PHP_INT_MAX`:

- 32bit-system: 2147483647 === 2 \*\* 31 - 1
- 64bit-system: 9223372036854775807 === 2 \*\* 63 - 1

8. Usage
--------

[](#8-usage)

- [8.1. Basic Usage](#81-basic-usage)
    - [8.1.1. Combination](#811-combination)
    - [8.1.2. CombinationGenerator](#812-combinationgenerator)
- [8.2. Using Combination](#82-using-combination)
- [8.3. Using Combination with Sorting](#83-using-combination-with-sorting)
- [8.4. Using CombinationGenerator](#84-using-combinationgenerator)
- [8.5. Using In PHPUnit](#85-using-in-phpunit)

### 8.1. Basic Usage

[](#81-basic-usage)

#### 8.1.1 Combination

[](#811-combination)

- PHP:

    ```
