Я пытаюсь найти все комбинации предметов в нескольких массивах. Количество массивов является случайным (это может быть 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] => Массив
(
[0] => синий
[1] => L
[2] => Половина селевов
)
[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] => Массив
(
[0] => синий, красный, зеленый
[1] => L, M
[2] => Половина селевов
) * * Тысяча двадцать-один