elminson / permutations
The Permutations generator package
v1.0.1
2023-12-04 18:50 UTC
Requires (Dev)
- phpunit/phpunit: 10.5.x-dev
README
A convenient way to generate permutations of arrays.
Ex.
// Customizable features for a smartphone $colors = ['Black', 'White']; $storageCapacities = ['64GB', '128GB']; // Generating variations using Permutations::generate $actualVariations = Permutations::generate([$colors, $storageCapacities]);
Result
Array ( [0] => Array ( [0] => Black [1] => 64GB ) [1] => Array ( [0] => Black [1] => 128GB ) [2] => Array ( [0] => White [1] => 64GB ) [3] => Array ( [0] => White [1] => 128GB ) )
You can also include the original array by doing
$includeOriginalArray = true; $array = ['AB', 'CD']; $array1 = ['EF', 'GH']; $permutationsExpected = [ ['AB', 'CD'], ['EF', 'GH'], ['AB', 'EF'], ['AB', 'GH'], ['CD', 'EF'], ['CD', 'GH']]; $permutationsElements = Arr::permutations([$array, $array1], $includeOriginalArray);
Result
Array ( [0] => Array ( [0] => AB [1] => CD ) [1] => Array ( [0] => EF [1] => GH ) [2] => Array ( [0] => AB [1] => EF ) [3] => Array ( [0] => AB [1] => GH ) [4] => Array ( [0] => CD [1] => EF ) [5] => Array ( [0] => CD [1] => GH ) )