Math_Combinatorics
This package is now a part of PEAR
A package that returns all the combinations and permutations, without repitition, of a given set and subset size. Associative arrays are preserved.
Combinations
require_once 'Math/Combinatorics.php';
$combinatorics = new Math_Combinatorics; var_dump($combinatorics->combinations(array( 'one' => 'a', 'two' => 'b', 'three' => 'c', 'four' => 'd', ), 3));
array(4) { [0]=> array(3) { ["one"]=> string(1) "a" ["two"]=> string(1) "b" ["three"]=> string(1) "c" } [1]=> array(3) { ["one"]=> string(1) "a" ["two"]=> string(1) "b" ["four"]=> string(1) "d" } [2]=> array(3) { ["one"]=> string(1) "a" ["three"]=> string(1) "c" ["four"]=> string(1) "d" } [3]=> array(3) { ["two"]=> string(1) "b" ["three"]=> string(1) "c" ["four"]=> string(1) "d" } }
Permutations
require_once 'Math/Combinatorics.php';
$combinatorics = new Math_Combinatorics;
var_dump($combinatorics->permutations(array(
'one' => 'a',
'two' => 'b',
'three' => 'c',
'four' => 'd',
), 3));
array(24) { [0]=> array(3) { ["one"]=> string(1) "a" ["two"]=> string(1) "b" ["three"]=> string(1) "c" } [1]=> array(3) { ["one"]=> string(1) "a" ["three"]=> string(1) "c" ["two"]=> string(1) "b" } [2]=> array(3) { ["two"]=> string(1) "b" ["three"]=> string(1) "c" ["one"]=> string(1) "a" } [3]=> array(3) { ["two"]=> string(1) "b" ["one"]=> string(1) "a" ["three"]=> string(1) "c" } [4]=> array(3) { ["three"]=> string(1) "c" ["one"]=> string(1) "a" ["two"]=> string(1) "b" } [5]=> array(3) { ["three"]=> string(1) "c" ["two"]=> string(1) "b" ["one"]=> string(1) "a" } [6]=> array(3) { ["one"]=> string(1) "a" ["two"]=> string(1) "b" ["four"]=> string(1) "d" } [7]=> array(3) { ["one"]=> string(1) "a" ["four"]=> string(1) "d" ["two"]=> string(1) "b" } [8]=> array(3) { ["two"]=> string(1) "b" ["four"]=> string(1) "d" ["one"]=> string(1) "a" } [9]=> array(3) { ["two"]=> string(1) "b" ["one"]=> string(1) "a" ["four"]=> string(1) "d" } [10]=> array(3) { ["four"]=> string(1) "d" ["one"]=> string(1) "a" ["two"]=> string(1) "b" } [11]=> array(3) { ["four"]=> string(1) "d" ["two"]=> string(1) "b" ["one"]=> string(1) "a" } [12]=> array(3) { ["one"]=> string(1) "a" ["three"]=> string(1) "c" ["four"]=> string(1) "d" } [13]=> array(3) { ["one"]=> string(1) "a" ["four"]=> string(1) "d" ["three"]=> string(1) "c" } [14]=> array(3) { ["three"]=> string(1) "c" ["four"]=> string(1) "d" ["one"]=> string(1) "a" } [15]=> array(3) { ["three"]=> string(1) "c" ["one"]=> string(1) "a" ["four"]=> string(1) "d" } [16]=> array(3) { ["four"]=> string(1) "d" ["one"]=> string(1) "a" ["three"]=> string(1) "c" } [17]=> array(3) { ["four"]=> string(1) "d" ["three"]=> string(1) "c" ["one"]=> string(1) "a" } [18]=> array(3) { ["two"]=> string(1) "b" ["three"]=> string(1) "c" ["four"]=> string(1) "d" } [19]=> array(3) { ["two"]=> string(1) "b" ["four"]=> string(1) "d" ["three"]=> string(1) "c" } [20]=> array(3) { ["three"]=> string(1) "c" ["four"]=> string(1) "d" ["two"]=> string(1) "b" } [21]=> array(3) { ["three"]=> string(1) "c" ["two"]=> string(1) "b" ["four"]=> string(1) "d" } [22]=> array(3) { ["four"]=> string(1) "d" ["two"]=> string(1) "b" ["three"]=> string(1) "c" } [23]=> array(3) { ["four"]=> string(1) "d" ["three"]=> string(1) "c" ["two"]=> string(1) "b" } }
|