0

I have a dynamically created array $array=[2,3,2]. I want to loop through it and get all the possible permutations.

$array = [2,3,2];
$c = count($array);
for($i=0; $i<$c; $i++) {
    for($j=0; $j<$array[$i]; $j++) {
        echo ($i+1).'-'.($j+1).'<br>';
    }
}

The result should be something like:

1-1-1
1-1-2
1-2-1
1-2-2
1-3-1
1-3-2
2-1-1
2-1-2
2-2-1
2-2-2
2-3-1
2-3-2

but it returns that:

1-1
1-2
2-1
2-2
2-3
3-1
3-2
eskimopest
  • 401
  • 3
  • 18

1 Answers1

0

I hope this ll help you :

$array = [2, 3, 2];
for ($i = 1; $i <= $array[0]; $i++) {
  for ($j = 1; $j <= $array[1]; $j++) {
    for ($k = 1; $k <= $array[2]; $k++) {
          echo $i,"-",$j,"-",$k,"<br>";
    }
  }
}
Omi
  • 3,954
  • 5
  • 21
  • 41
  • tks =) for the question you're correct, but i have a litle problem. my array has 3 elements but as it is dymamic, it can have 4, 5, 6... elements (something like $array = [2, 3, 2, 3, 2, ...]). How can i make it every possible cominations depending on the array length? – eskimopest Mar 14 '17 at 19:32