3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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; } $x = powerSet(array(1,2,3,4,5)); var_dump($x);
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
array(31) { [0]=> array(1) { [0]=> int(5) } [1]=> array(1) { [0]=> int(4) } [2]=> array(2) { [0]=> int(4) [1]=> int(5) } [3]=> array(1) { [0]=> int(3) } [4]=> array(2) { [0]=> int(3) [1]=> int(5) } [5]=> array(2) { [0]=> int(3) [1]=> int(4) } [6]=> array(3) { [0]=> int(3) [1]=> int(4) [2]=> int(5) } [7]=> array(1) { [0]=> int(2) } [8]=> array(2) { [0]=> int(2) [1]=> int(5) } [9]=> array(2) { [0]=> int(2) [1]=> int(4) } [10]=> array(3) { [0]=> int(2) [1]=> int(4) [2]=> int(5) } [11]=> array(2) { [0]=> int(2) [1]=> int(3) } [12]=> array(3) { [0]=> int(2) [1]=> int(3) [2]=> int(5) } [13]=> array(3) { [0]=> int(2) [1]=> int(3) [2]=> int(4) } [14]=> array(4) { [0]=> int(2) [1]=> int(3) [2]=> int(4) [3]=> int(5) } [15]=> array(1) { [0]=> int(1) } [16]=> array(2) { [0]=> int(1) [1]=> int(5) } [17]=> array(2) { [0]=> int(1) [1]=> int(4) } [18]=> array(3) { [0]=> int(1) [1]=> int(4) [2]=> int(5) } [19]=> array(2) { [0]=> int(1) [1]=> int(3) } [20]=> array(3) { [0]=> int(1) [1]=> int(3) [2]=> int(5) } [21]=> array(3) { [0]=> int(1) [1]=> int(3) [2]=> int(4) } [22]=> array(4) { [0]=> int(1) [1]=> int(3) [2]=> int(4) [3]=> int(5) } [23]=> array(2) { [0]=> int(1) [1]=> int(2) } [24]=> array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(5) } [25]=> array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(4) } [26]=> array(4) { [0]=> int(1) [1]=> int(2) [2]=> int(4) [3]=> int(5) } [27]=> array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } [28]=> array(4) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(5) } [29]=> array(4) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) } [30]=> array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) } }