$num = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
在数组 $num 中有 10 个数字。我想用 PHP 列出所有可能的 3 位数组合。就像:
1, 2, 3
1, 2, 4
1, 2, 5
etc
编辑:
感谢所有答案,但如果我想创建 6 个数字组合,则会出现问题。
$num_array2 = array(1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 15, 16, 18, 19, 20);
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
etc.
我做错什么了吗?