编辑:有点背景知识。我们有一个接受6个不同变量的模块。我们正在尝试完全自动化测试和微调模块可能接受的不同值。
我有6个数组,需要获得所有可能性的组合。
$words[1] = array("A","B","C","D","E","F");
$words[2] = array("Aa","Bb","Cc","Dd","Ee","Ff");
$words[3] = array("Aq","Bq","Cq","Dq","Eq","Fq");
$words[4] = array("Ab","Bc","Cd","De","Ef","F");
$words[5] = array("Az","Bz","Cz","Dz","Ez","Fz");
$words[6] = array("A1","B1","C1","D1","E1","F1");
所以基本上我正在寻找所有可能的独特组合,同时仍然保持$ words数组的顺序。
示例:
C Bb Fq De Bz B1
C Bb Fq De Bz E1
C Bb Fq De Fz B1
答案 0 :(得分:1)
这应该可以提供所需的输出。
for($a=0; $a < count($words[1]); $a++) {
for($b=0; $b < count($words[2]); $b++) {
for($c=0; $c < count($words[3]); $c++) {
for($d=0; $d < count($words[4]); $d++) {
for($e=0; $e < count($words[5]); $e++) {
for($f=0; $f < count($words[6]); $f++) {
echo $words[1][$a] . " " . $words[2][$b] . " " . $words[3][$c] . " " . $words[4][$d] . " " . $words[5][$e] . " " . $words[6][$f] . "<br />";
}
}
}
}
}
}
答案 1 :(得分:1)
xdazz有正确的方法,但执行错误......你需要创建一个字符串:
$ret = array();
foreach ($words[1] as $word1) {
foreach ($words[2] as $word2) {
foreach ($words[3] as $word3) {
foreach ($words[4] as $word4) {
foreach ($words[5] as $word5) {
foreach ($words[6] as $word6) {
$ret[] = $word1.' '.$word2.' '.$word3.' '.$word4.' '.$word5.' '.$word6;
}
}
}
}
}
}
print_r($ret);