Permutation
Platform: PHP
Published Oct 20, 2011
Updated Jun 29, 2012
Permute an array of values.
The nature of the input allows you to pass either a string or an int into the function as a singular value:
I'd advise staying under 9 values, as the amount of permutations required is
You can see a list of amounts here:
http://www4d.wolframalpha.com/Calculate/MSP/MSP11619hfc32hdb0fb84f00002gb116g73ib5b7g0?MSPStoreType=image/gif&s=31&w=350&h=1008 class permutation {
public $permutations = array();
function __construct(array $arr) { $this->showPerms($this->permute($arr)); }
private function showPerms($a,$i='') {
if (is_array($a))
foreach($a as $k => $v)
$this->showPerms($v,$i.$k);
else
$this->results[] = $i.$a;
}
private function permute(array $arr) {
$out = array();
if (count($arr) > 1)
foreach($arr as $r => $c) {
$n = $arr;
unset($n[$r]);
$out[$c] = $this->permute($n);
}
else
return array_shift($arr);
return $out;
}
}