portavice / permutation
Generating permutations of arrays normal and recursive
Installs: 1 018
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: ^8.0
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^0.7.2
- phpcompatibility/php-compatibility: ^9.3.5
- phpunit/phpunit: ^10.2.2
- squizlabs/php_codesniffer: ^3.7.2
README
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.
Installation
To install this package with Composer:
To install it, just add the following to your composer.json
file:
composer require portavice/permutation
Methods
Usage
<?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! }
License
This library is licensed under the MIT license.
Author
This library was written by Shaun Lüdeke for Portavice GmbH.
Development
How to develop
- Run
composer install
to install the dependencies for PHP. - Run
composer test
to run all PHPUnit tests. - Run
composer cs
to check compliance with the code style andcomposer csfix
to fix code style violations before every commit.
Code Style
PHP code MUST follow PSR-12 specification.
We use PHP_CodeSniffer for the PHP code style check.