-1

I have an array like

$arr = array(
[1,2,3],
[4,5,6,7,8]
);

I want print the result as a permutation,

1,4
1,5
1,6
1,7
1,8

2,4
2,5
2,6
2,7
2,8

3,4
3,5
3,6
3,7
3,8

I tried but more confusing for me. This is just simple array, here it has only 2 inner arrays, but it may be more.

Sugumar Venkatesan
  • 4,019
  • 8
  • 46
  • 77

1 Answers1

0

Use below to achieve this :

list($array1,$array2) = array(
            [1,2,3],
            [4,5,6,7,8]
        );

     foreach ($array1 as $value1){
         foreach ($array2 as $value2){
             echo $value1.",".$value2."<br>";
         }
            echo "<br>";
     }

You will get exactly same result as you want.

akshaypjoshi
  • 1,245
  • 1
  • 15
  • 24
  • This answer is not sufficient, OP clearly saying: _here it has only 2 inner arrays, but it may be more._ So he needs a (recursive) solution that allows for changing structure. – jibsteroos Feb 14 '19 at 13:45
  • So if there are three inner arrays, how will you create permutations between them? – akshaypjoshi Feb 14 '19 at 13:47