Skip to content

Instantly share code, notes, and snippets.

@jsifalda
Created December 11, 2012 15:59
Show Gist options
  • Save jsifalda/4259637 to your computer and use it in GitHub Desktop.
Save jsifalda/4259637 to your computer and use it in GitHub Desktop.
Get array of variations from charlist
<?php
function powerSet($in,$minLength = 1) {
$count = count($in);
$members = pow(2,$count);
$return = array();
for ($i = 0; $i < $members; $i++) {
$b = sprintf("%0".$count."b",$i);
$out = array();
for ($j = 0; $j < $count; $j++) {
if ($b{$j} == '1') $out[] = $in[$j];
}
if (count($out) >= $minLength) {
$return[] = $out;
}
}
return $return;
}
print_r(powerSet(array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', '...'), 13));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment