PHP数组任意N个数相加等于一个定数
/** * @param array $cc 所需数组 * @param int $val 指定值 * @return array */ function demo($cc, $val) { for ($i = 1; $i < 1 << count($cc); $i++) { $sum = 0; $temp = ""; for ($j = 0; $j < count($cc); $j++) { if (($i & 1 << $j) != 0) { $sum += $cc[$j]; $temp .= $cc[$j] . "+"; } } if ($sum == $val) { $arr[] = trim($temp, '+'); } } return $arr; } $cc = array(12, 12, 5, 9, 12, 5, 7, 5, 7, 9, 12,); echo "<pre>"; print_r(demo($cc, 25));