我试图找到几个数组中的所有项目组合。数组的数量是随机的(可以是2,3,4,5 ......)。每个数组中的元素数量也是随机的......
例如,我有3个数组:
$arrayA = array('A1','A2','A3',,'A4','A5','A6'); // (3 From here each time)
$arrayB = array('B1','B2','B3','B4','B5'); //(2 From here each time)
$arrayC = array('C1','C2'); // (1 From here each time)
我想生成一个数组:
A1+A3+A5, B1+B5, C1
A1+A4+A5, B1+B4, C1
....
我尝试过这样但我需要从数组中获取不同的元素数而不是每个数组中的一个元素。
<?php
$color = array('Blue','Red','Black','Green');
$size = array('L','M','S','XL','XXL');
$type = array('Half selevs','full seleves');
$options = [
$color,
$size,
$type,
];
$combinations = getCombinations($options);
print_r($combinations);
function getCombinations($options){
$combinations = [[]];
for ($count = 0; $count < count($options); $count++) {
$tmp = [];
foreach ($combinations as $v1) {
foreach ($options[$count] as $v2)
$tmp[] = array_merge($v1, [$v2]);
}
$combinations = $tmp;
}
return $combinations;
}
?>
输出:
阵 ( [0] =&gt;排列 ( [0] =&gt;蓝色 [1] =&gt;大号 [2] =&gt;半赛程 )
[1] => Array
(
[0] => Blue
[1] => L
[2] => full seleves
)
[2] => Array
(
[0] => Blue
[1] => M
[2] => Half selevs
)
[3] => Array
(
[0] => Blue
[1] => M
[2] => full seleves
)
[4] => Array
(
[0] => Blue
[1] => S
[2] => Half selevs
)
[5] => Array
(
[0] => Blue
[1] => S
[2] => full seleves
)
[6] => Array
(
[0] => Blue
[1] => XL
[2] => Half selevs
)
[7] => Array
(
[0] => Blue
[1] => XL
[2] => full seleves
)
[8] => Array
(
[0] => Blue
[1] => XXL
[2] => Half selevs
)
[9] => Array
(
[0] => Blue
[1] => XXL
[2] => full seleves
)
[10] => Array
(
[0] => Red
[1] => L
[2] => Half selevs
)
[11] => Array
(
[0] => Red
[1] => L
[2] => full seleves
)
[12] => Array
(
[0] => Red
[1] => M
[2] => Half selevs
)
[13] => Array
(
[0] => Red
[1] => M
[2] => full seleves
)
[14] => Array
(
[0] => Red
[1] => S
[2] => Half selevs
)
[15] => Array
(
[0] => Red
[1] => S
[2] => full seleves
)
[16] => Array
(
[0] => Red
[1] => XL
[2] => Half selevs
)
[17] => Array
(
[0] => Red
[1] => XL
[2] => full seleves
)
[18] => Array
(
[0] => Red
[1] => XXL
[2] => Half selevs
)
[19] => Array
(
[0] => Red
[1] => XXL
[2] => full seleves
)
[20] => Array
(
[0] => Black
[1] => L
[2] => Half selevs
)
[21] => Array
(
[0] => Black
[1] => L
[2] => full seleves
)
[22] => Array
(
[0] => Black
[1] => M
[2] => Half selevs
)
[23] => Array
(
[0] => Black
[1] => M
[2] => full seleves
)
[24] => Array
(
[0] => Black
[1] => S
[2] => Half selevs
)
[25] => Array
(
[0] => Black
[1] => S
[2] => full seleves
)
[26] => Array
(
[0] => Black
[1] => XL
[2] => Half selevs
)
[27] => Array
(
[0] => Black
[1] => XL
[2] => full seleves
)
[28] => Array
(
[0] => Black
[1] => XXL
[2] => Half selevs
)
[29] => Array
(
[0] => Black
[1] => XXL
[2] => full seleves
)
[30] => Array
(
[0] => Green
[1] => L
[2] => Half selevs
)
[31] => Array
(
[0] => Green
[1] => L
[2] => full seleves
)
[32] => Array
(
[0] => Green
[1] => M
[2] => Half selevs
)
[33] => Array
(
[0] => Green
[1] => M
[2] => full seleves
)
[34] => Array
(
[0] => Green
[1] => S
[2] => Half selevs
)
[35] => Array
(
[0] => Green
[1] => S
[2] => full seleves
)
[36] => Array
(
[0] => Green
[1] => XL
[2] => Half selevs
)
[37] => Array
(
[0] => Green
[1] => XL
[2] => full seleves
)
[38] => Array
(
[0] => Green
[1] => XXL
[2] => Half selevs
)
[39] => Array
(
[0] => Green
[1] => XXL
[2] => full seleves
)
)
我想要这样:
阵 ( [0] =&gt;排列 ( [0] =&gt;蓝,红,绿 [1] =&gt; L,M [2] =&gt;半赛程 )
答案 0 :(得分:0)
你可以这样做......
<?php
$variants = array();
$possible_letters = array('A','B','C');
$max_variant_number = 6;
foreach ($possible_letters as $possible_letter) {
$min_variant_number = 0;
while ($min_variant_number <= $max_variant_number) {
$variants[] = $possible_letters.$min_variant_number;
$min_variant_number++;
}
}
这只是2个循环,它们在数组和持续增加的数字之间进行一些连接,直到数字已经耗尽,然后它移动到下一个字母并重置该数字。请注意它是一个嵌套循环,所以 N平方顺序将适用于复杂性理论。