This is a simple permutation library for PHP.
It can be used to generate all possible permutations of a given array.
It can also be used to generate all possible permutations of a given array recursively.
To install this package with Composer:
To install it, just add the following to your composer.json file:
composer require portavice/permutation| Method | Static | Recursive |
|---|---|---|
permutate() |
No | No |
getPermutations(array $input, bool $withSort = false) |
Yes | No |
getPermutationsWithCallback(array $input, callable $callback, bool $unsetAfterCall = false, mixed ...$args) |
Yes | No |
permutateRecursive() |
No | Yes |
getPermutationsRecursive(array $input, bool $withSort = false) |
Yes | Yes |
getPermutationsRecursiveWithCallback(array $input, callable $callback, bool $unsetAfterCall = false, mixed ...$args) |
Yes | Yes |
getResult(bool $sorted = false) |
No | |
setOffset(int $offset) |
No | |
setLimit(int $limit) |
No | |
setCallback(callable $callback, bool $unsetAfterCall = false, mixed ...$args) |
No |
<?php
require_once 'vendor/autoload.php';
use Portavice\Permutation\Permutation;
// You can also use the static method:
$permutations = Permutation::getPermutations(
[
'a' => ['a1', 'a2'],
'b' => ['b1', 'b2'],
'c' => ['c1', 'c2'],
]
);
// Output:
// [
// ['a' => 'a1', 'b' => 'b1', 'c' => 'c1'],
// ['a' => 'a1', 'b' => 'b1', 'c' => 'c2'],
// ['a' => 'a1', 'b' => 'b2', 'c' => 'c1'],
// ['a' => 'a1', 'b' => 'b2', 'c' => 'c2'],
// ['a' => 'a2', 'b' => 'b1', 'c' => 'c1'],
// ['a' => 'a2', 'b' => 'b1', 'c' => 'c2'],
// ['a' => 'a2', 'b' => 'b2', 'c' => 'c1'],
// ['a' => 'a2', 'b' => 'b2', 'c' => 'c2'],
// ]
// You can also use the recursive method:
$permutations = Permutation::getPermutationsRecursive(
[
'a' => ['a1', 'a2'],
'b' => ['b1', 'b2'],
'c' => ['c1', 'c2'],
]
);
// Output:
// [
// ['a' => 'a1'],
// ['a' => 'a2'],
// ['b' => 'b1'],
// ['b' => 'b2'],
// ['c' => 'c1'],
// ['c' => 'c2'],
// ['a' => 'a1', 'b' => 'b1'],
// ['a' => 'a1', 'b' => 'b2'],
// ['a' => 'a1', 'c' => 'c1'],
// ['a' => 'a1', 'c' => 'c2'],
// ['a' => 'a2', 'b' => 'b1'],
// ['a' => 'a2', 'b' => 'b2'],
// ['a' => 'a2', 'c' => 'c1'],
// ['a' => 'a2', 'c' => 'c2'],
// ['b' => 'b1', 'c' => 'c1'],
// ['b' => 'b1', 'c' => 'c2'],
// ['b' => 'b2', 'c' => 'c1'],
// ['b' => 'b2', 'c' => 'c2'],
// ['a' => 'a1', 'b' => 'b1', 'c' => 'c1'],
// ['a' => 'a1', 'b' => 'b1', 'c' => 'c2'],
// ['a' => 'a1', 'b' => 'b2', 'c' => 'c1'],
// ['a' => 'a1', 'b' => 'b2', 'c' => 'c2'],
// ['a' => 'a2', 'b' => 'b1', 'c' => 'c1'],
// ['a' => 'a2', 'b' => 'b1', 'c' => 'c2'],
// ['a' => 'a2', 'b' => 'b2', 'c' => 'c1'],
// ['a' => 'a2', 'b' => 'b2', 'c' => 'c2'],
// ]
// NEW: Generators! For memory constrained environments.
// More info how to use generators here: https://www.php.net/manual/en/language.generators.overview.php
$permutations = Permutation::getGenerator([
'a' => ['a1', 'a2'],
'b' => ['b1', 'b2'],
'c' => ['c1', 'c2'],
]);
foreach ($permutations as $permutation) {
// ... do stuff here!
}This library is licensed under the MIT license.
This library was written by Shaun Lüdeke for portavice GmbH.
- Run
composer installto install the dependencies for PHP. - Run
composer testto run all PHPUnit tests. - Run
composer csto check compliance with the code style andcomposer csfixto fix code style violations before every commit.
PHP code MUST follow PSR-12 specification.
We use Laravel Pint for the PHP code style check.