I have the nested array structure below.
$letters = array(
$A = array(1,2),
$B = array(3,4),
$C = array(5,6)
);
My goal is to find all possible permutations of the numbers, while the letters must keep the order A-B-C. The expected output is:
1-3-5
1-3-6
1-4-5
1-4-6
2-3-5
2-3-6
2-4-5
2-4-6
Of course, this could easily be accomplished with foreach:
foreach($A as $a){
foreach($B as $b){
foreach($C as $c){
echo $a.$b.$c.'<br>';
}
}
}
However, I want this to work dynamically, with a varying number of arrays. I already figured that a recursive approach could work, but I cannot wrap my head around it. Can someone help?
$lettersarray looks weird. Wouldn't$letters = array(array(1, 2), array(3, 4), array(5, 6));be enough?