Answer the question
In order to leave comments, you need to log in
How to get all unique pairs from an array?
There is an array containing arrays with strings inside. For example:
$array = [
['Синий', 'Алый', 'Коричневый'],
['Дерево', 'Сталь', 'Железо'],
['Корабль', 'Самолет', 'Поезд'],
...
];
Answer the question
In order to leave comments, you need to log in
$numArr = count($array);
$numElems = count($array[0]);
$combinations = [];
$numCombinations = pow($numElems, $numArr);
for ($i = 0; $i < $numCombinations; $i++) {
$combination = [];
for ($j = 0; $j < $numArr; $j++) {
$arrIndex = $j;
$elIndex = $i / pow($numElems, $j) % $numElems;
$combination[] = $array[$arrIndex][$elIndex];
}
$combinations[] = $combination;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question