- <?php
- $arr = array(1,2,3,4,5,6,7,8,9,10,11,12);
- $n = count($arr);
- $sub_n = pow(2,$n);
- $sub_array = array();
- for($i=0; $i<$sub_n; $i++){
- $m = sprintf('%0+'.$n.'b',$i);
- $t_arr = array();
- for($j=0;$j<$n;$j++)
- if($m{$j}==1 && $j!=$n) $t_arr[] = $arr[$j];
- if(array_sum($t_arr)==12) echo '{'.implode(',', $t_arr).'}';
- }
- ?>
输出结果为:{12}{5,7}{4,8}{3,9}{3,4,5}{2,10}{2,4,6}{2,3,7}{1,11}{1,5,6}{1,4,7}{1,3,8}{1,2,9}{1,2,4,5}{1,2,3,6}