over 9 years ago
Hello Reader's! if you have set of arrays and you want to get all of them seperatly then the code below will help you.
Let's say if you have
[ [a], [b], [c], [a, b], [a, c], [b, c], [a, b, c] ]
and you want this
The code will go like this:-
- 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;
- }
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; }
0 Comment(s)