这是“如何列出所有可能的组合? ”的扩展问题。对于最后一个问题,它显示了所有排列而不是所有组合。
$num_array2 = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
foreach($num_array2 AS $value1) {
foreach($num_array2 AS $value2) {
if($value2 == $value1) {
continue;
}
foreach($num_array2 AS $value3) {
if($value3 == $value1 || $value3 == $value2) {
continue;
}
foreach($num_array2 AS $value4) {
if($value4 == $value1 || $value4 == $value2 || $value4 == $value3) {
continue;
}
foreach($num_array2 AS $value5) {
if($value5 == $value1 || $value5 == $value2 || $value5 == $value3 || $value5 == $value4) {
continue;
}
foreach($num_array2 AS $value6) {
if($value6 == $value1 || $value6 == $value2 || $value6 == $value3 || $value6 == $value4 || $value6 == $value5) {
continue;
}
echo "$value1, $value2, $value3, $value4, $value5, $value6 \n<br />";
}
}
}
}
}
}
结果将是:
1, 2, 3, 4, 5, 6
1, 2, 3, 4, 5, 7
1, 2, 3, 4, 5, 8
1, 2, 3, 4, 5, 9
1, 2, 3, 4, 5, 10
1, 2, 3, 4, 6, 5
1, 2, 3, 4, 6, 7
1, 2, 3, 4, 6, 8
1, 2, 3, 4, 6, 9
1, 2, 3, 4, 6, 10
如您所见,重复了“1、2、3、4、5、6”和“1、2、3、4、6、5”(尽管顺序不同)。我不希望显示像“1、2、3、4、6、5”这样的结果。我应该如何修改代码?