I have an array that looks like this:
$i[0] = ['a', 'b', 'c'];
$i[1] = ['d', 'e'];
$i[2] = ['a', 'b', 'c'];
$i[3] = ['d', 'e'];
$i[4] = ['f', 'g', 'h'];
I want to get all the possible permutations or combinations of this array, but without using the same value twice from two or more sub-arrays. So for instance, the result a d b e f
would be possible, but not a d a d f
.
I have tried basic permutation algorithms, but I can't wrap my head around how to modify it to do what I want.
Here's what I've got currently:
function array_permutation(array $a){
$count = array_map('count', $a);
$finalSize = 1;
foreach ($count as $val) {
$finalSize *= $val;
}
$output = [];
for ($i = 0; $i < $finalSize; $i++) {
$output[$i] = [];
for ($c = 0; $c < count($a); $c++) {
$index = ($i + $finalSize) % $count[$c];
array_push($output[$i], $a[$c][$index]);
}
}
return $output;
}