Skip to content

portavice/Permutation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Permutation

Latest Version on Packagist Test Status Code Style Status PHP from Packagist Total Downloads

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

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

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 and composer 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.