我有一个代码给我可能的组合,但它缺少每个数组的组合。请告诉我如何生成每个阵列的组合。喜欢在运行声明的代码之后,我将获得可能的组合....但我需要像(粗糙,中等,小)的组合......
$array1 = array('rough', 'smooth', 'coarse');
$array2 = array('shiny', 'matte', 'rough');
$array3 = array('very large', 'large', 'medium', 'small');
foreach($array1 as $i)
foreach($array2 as $j)
foreach($array3 as $k)
$output[] = "$i, $j, $k";
var_dump($output);
答案 0 :(得分:1)
我想这就是你想要的
$array1 = array('rough', 'smooth', 'coarse');
$array2 = array('shiny', 'matte', 'rough');
$array3 = array('very large', 'large', 'medium', 'small');
generateCcombination(array($array1,$array2,$array3));
function generateCcombination($array) {
global $codes, $pos;
if(count($array)) {
for($i=0; $i<count($array[0]); $i++) {
$tmp = $array;
$codes[$pos] = $array[0][$i];
$tarr = array_shift($tmp);
$pos++;
generateCcombination($tmp);
}
} else {
echo join(", ", $codes)."<br/>";
}
$pos--;
}
输出
rough, shiny, very large
rough, shiny, large
rough, shiny, medium
rough, shiny, small
rough, matte, very large
rough, matte, large
rough, matte, medium
rough, matte, small
rough, rough, very large
rough, rough, large
rough, rough, medium
rough, rough, small
rough, shiny, very large, smooth
rough, shiny, large, smooth
rough, shiny, medium, smooth
rough, shiny, small, smooth
rough, matte, very large, smooth
rough, matte, large, smooth
rough, matte, medium, smooth
rough, matte, small, smooth
rough, rough, very large, smooth
rough, rough, large, smooth
rough, rough, medium, smooth
rough, rough, small, smooth
rough, shiny, very large, coarse
rough, shiny, large, coarse
rough, shiny, medium, coarse
rough, shiny, small, coarse
rough, matte, very large, coarse
rough, matte, large, coarse
rough, matte, medium, coarse
rough, matte, small, coarse
rough, rough, very large, coarse
rough, rough, large, coarse
rough, rough, medium, coarse
rough, rough, small, coarse
答案 1 :(得分:1)
问题不是很清楚,但在我看来,你希望在组合中多次使用每个数组。试试这个:
$array1 = array('rough', 'smooth', 'coarse');
$array2 = array('shiny', 'matte', 'rough');
$array3 = array('very large', 'large', 'medium', 'small');
$combinedArray=array_merge($array1, $array2, $array3);
foreach($combinedArray as $i)
foreach($combinedArray as $j)
foreach($combinedArray as $k)
$output[] = "$i, $j, $k";
var_dump($output);
答案 2 :(得分:1)
$array1 = array('rough', 'smooth', 'coarse');
$array2 = array('shiny', 'matte', 'rough');
$array3 = array('very large', 'large', 'medium', 'small');
$array=array_merge($array1,$array2,$array3);
$combinations=array();
for($x=0;$x<(count($array)-2);$x++) {
$a=$array[$x];
for ($y=$x+1;$y<count($array);$y++) {
$b=$array[$y];
for ($z=$y+1;$z<count($array);$z++) {
$c=$array[$z];
$combinations[]="$a, $b, $c";
}
}
}
这将输出所有值的所有可能组合,而不重复任何值。如下图所示:
Array
(
[0] => rough, smooth, coarse
[1] => rough, smooth, shiny
[2] => rough, smooth, matte
[3] => rough, smooth, rough
[4] => rough, smooth, very large
[5] => rough, smooth, large
[6] => rough, smooth, medium
[7] => rough, smooth, small
[8] => rough, coarse, shiny
[9] => rough, coarse, matte
[10] => rough, coarse, rough
[11] => rough, coarse, very large
[12] => rough, coarse, large
[13] => rough, coarse, medium
[14] => rough, coarse, small
[15] => rough, shiny, matte
[16] => rough, shiny, rough
[17] => rough, shiny, very large
[18] => rough, shiny, large
[19] => rough, shiny, medium
[20] => rough, shiny, small
[21] => rough, matte, rough
[22] => rough, matte, very large
[23] => rough, matte, large
[24] => rough, matte, medium
[25] => rough, matte, small
[26] => rough, rough, very large
[27] => rough, rough, large
[28] => rough, rough, medium
[29] => rough, rough, small
[30] => rough, very large, large
[31] => rough, very large, medium
[32] => rough, very large, small
[33] => rough, large, medium
[34] => rough, large, small
[35] => rough, medium, small
[36] => smooth, coarse, shiny
[37] => smooth, coarse, matte
[38] => smooth, coarse, rough
[39] => smooth, coarse, very large
[40] => smooth, coarse, large
[41] => smooth, coarse, medium
[42] => smooth, coarse, small
[43] => smooth, shiny, matte
[44] => smooth, shiny, rough
[45] => smooth, shiny, very large
[46] => smooth, shiny, large
[47] => smooth, shiny, medium
[48] => smooth, shiny, small
[49] => smooth, matte, rough
[50] => smooth, matte, very large
[51] => smooth, matte, large
[52] => smooth, matte, medium
[53] => smooth, matte, small
[54] => smooth, rough, very large
[55] => smooth, rough, large
[56] => smooth, rough, medium
[57] => smooth, rough, small
[58] => smooth, very large, large
[59] => smooth, very large, medium
[60] => smooth, very large, small
[61] => smooth, large, medium
[62] => smooth, large, small
[63] => smooth, medium, small
[64] => coarse, shiny, matte
[65] => coarse, shiny, rough
[66] => coarse, shiny, very large
[67] => coarse, shiny, large
[68] => coarse, shiny, medium
[69] => coarse, shiny, small
[70] => coarse, matte, rough
[71] => coarse, matte, very large
[72] => coarse, matte, large
[73] => coarse, matte, medium
[74] => coarse, matte, small
[75] => coarse, rough, very large
[76] => coarse, rough, large
[77] => coarse, rough, medium
[78] => coarse, rough, small
[79] => coarse, very large, large
[80] => coarse, very large, medium
[81] => coarse, very large, small
[82] => coarse, large, medium
[83] => coarse, large, small
[84] => coarse, medium, small
[85] => shiny, matte, rough
[86] => shiny, matte, very large
[87] => shiny, matte, large
[88] => shiny, matte, medium
[89] => shiny, matte, small
[90] => shiny, rough, very large
[91] => shiny, rough, large
[92] => shiny, rough, medium
[93] => shiny, rough, small
[94] => shiny, very large, large
[95] => shiny, very large, medium
[96] => shiny, very large, small
[97] => shiny, large, medium
[98] => shiny, large, small
[99] => shiny, medium, small
[100] => matte, rough, very large
[101] => matte, rough, large
[102] => matte, rough, medium
[103] => matte, rough, small
[104] => matte, very large, large
[105] => matte, very large, medium
[106] => matte, very large, small
[107] => matte, large, medium
[108] => matte, large, small
[109] => matte, medium, small
[110] => rough, very large, large
[111] => rough, very large, medium
[112] => rough, very large, small
[113] => rough, large, medium
[114] => rough, large, small
[115] => rough, medium, small
[116] => very large, large, medium
[117] => very large, large, small
[118] => very large, medium, small
[119] => large, medium, small
)