Download the PHP package macocci7/php-combination without Composer
On this page you can find all versions of the php package macocci7/php-combination. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download macocci7/php-combination
More information about macocci7/php-combination
Files in macocci7/php-combination
Package php-combination
Short Description A simple PHP library to make combinations from array elements.
License MIT
Informations about the package php-combination
PHP-Combination
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
- 1. Features
- 2. Contents
- 3. Requirements
- 4. Installation
- 5. Classes
- 5.1. Combination
- 5.2. CombinationGenerator
- 5.3. How to choose the class to use
- 6. Methods
- 7. Limit on the Number of Array Elements
- 8. Usage
- 8.1. Basic Usage
- 8.1.1. Combination
- 8.1.2. CombinationGenerator
- 8.2. Using Combination
- 8.3. Using Combination with Sorting
- 8.4. Using CombinationGenerator
- 8.5. Using In PHPUnit
- 9. Examples
- 10. LICENSE
3. Requirements
- PHP 8.1 or later
- Composer
4. Installation
5. Classes
There're 2 types of classes for the same methods.
5.1. Combination
This class returns the result as type of array
.
5.2. CombinationGenerator
This class returns the result as type of Generator
object.
5.3. How to choose the class to use
There might be 3 factors.
-
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:
$n formula elements 1 2 ** 1 - 1 1 5 2 ** 5 - 1 31 10 2 ** 10 - 1 1,023 20 2 ** 20 - 1 1,048,575 30 2 ** 30 - 1 1,073,741,823 40 2 ** 40 - 1 1,099,511,627,775 50 2 ** 50 - 1 1,125,899,906,842,623 60 2 ** 60 - 1 1,152,921,504,606,846,975 62 2 ** 62 - 1 4,611,686,018,427,387,903 -
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 thanCombinationGenerator
. -
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.1. Macocci7\PhpCombination\Combination
all()
: returns all combinations of the parampairs()
: returns all pairs of the paramofN()
: returns all combinations of N elements of the paramofA2B()
: returns all combinations of A to B elements of the paramfromArrays()
: returns all combinations of multiple arrays as an instance ofIterator
.
6.2. Macocci7\PhpCombination\CombinationGenenrator
all()
: returns all combinations of the parampairs()
: returns all pairs of the paramofN()
: returns all combinations of N elements of the paramofA2B()
: returns all combinations of A to B elements of the paramfromArrays()
: returns all combinations of multiple arrays as an instance ofIterator
.
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.1. Basic Usage
- 8.1.1. Combination
- 8.1.2. CombinationGenerator
- 8.2. Using Combination
- 8.3. Using Combination with Sorting
- 8.4. Using CombinationGenerator
- 8.5. Using In PHPUnit
8.1. Basic Usage
8.1.1 Combination
-
PHP:
- Result:
8.1.2. CombinationGenerator
-
PHP:
- Result:
8.2. Using Combination
-
PHP:
- Result:
8.3. Using Combination with Sorting
-
PHP:
- Result:
8.4. Using CombinationGenerator
-
PHP:
- Result:
8.5. Using In PHPUnit
For Example, fromArray()
method can be quite useful for testing with data providers of PHPUnit.
Here's an example for testing the class for ordering products,
with patterns of size
, color
and amount
.
-
Install MONOLOG before testing: (Just for this example)
-
PHP: Class to be tested
-
PHP: Test Class
-
Result: STDOUT
- Result: LOG
9. Examples
- BasicUsage.txt
- BasicUsageGenerator.txt
- UseCombination.txt
- UseCombinationSort.txt
- UseCombinationGenerator.txt
- UseInPhpUnit.log
10. LICENSE
MIT
Document Created: 2023/11/11
Document Updated: 2025/01/04
Copyright 2023 - 2025 macocci7