使用 PHP 組合數(shù)組中的單詞和數(shù)字
我想在我的結果中組合兩個數(shù)組我的代碼 <?php /* Designated level for each exp Level 2 - 23 exp Level 3 - 34 exp Level 4 - 45 exp Level 5 - 56 exp Level 6 - 68 exp Level 7 - 79 exp Level 8 - 90 exp Level 9 - 101 exp Level 10 - 112 exp Level 11 - 123 exp Level 12 - 134 exp Level 13 - 145 exp Level 14 - 156 exp Level 15 - 168 exp Level 16 - 179 exp */ $limit = 100000-99318; // Level $arrlevel = array ('Level 2','Level 3','Level 4','Level 5','Level 6','Level 7','Level 8','Level 9','Level 10','Level 11','Level 12','Level 13','Level 14','Level 15','Level 16'); // Exp $array = array (23,34,45,56,68,79,90,101,112,123,134,145,156,168,179); $array = array_filter($array, function($var) use ($limit) { return ($var <= $limit); }); $num = count($array); $total = pow(2, $num); $out = array(); for ($i = 0; $i < $total; $i++) { $comb = array(); for ($j = 0; $j < $num; $j++) { // is bit $j set in $i? if (pow(2, $j) & $i){ $comb[] = $array[$j]; } } if (array_sum($comb) == $limit) { $out[] = $comb; } } array_multisort(array_map('count', $out), SORT_ASC, $out); $out = array_unique($out, SORT_REGULAR); $m = 1; foreach($out as $result) echo "<b>Possible Answer ". $m++. " : </b> " .implode(', ', $result)." <br><br>"; ?>輸出:可能的答案 1:23、34、45、68、79、90、112、179可能的答案 2:23、34、45、68、79、90、123、168可能的答案 3:23、34、45、68、79、101、112、168我想要這樣的輸出可能的答案 1:級別 2 - 23 | 級別 3 - 34 | 等級 4 - 45 | 等級 6 - 68 | 等級 7 - 79 | 級別 8 - 90 | 等級 10 - 112 | 等級 16 - 179-----------------------------------------我想組合兩個數(shù)組(這程序是關于找到所有組合以達到結果[子集求和程序])
查看完整描述