3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = [ [ 'rates' => [1, 2], 'origin' => 1, ], [ 'rates' => [3, 4], 'origin' => 2, ], [ 'rates' => [5], 'origin' => 3, ], ]; // нужно получить все возможные комбинации, как показано ниже $expected1 = [ [ [ 'rate' => 1, 'origin' => 1, ], [ 'rate' => 3, 'origin' => 2, ], [ 'rate' => 5, 'origin' => 3, ], ], [ [ 'rate' => 1, 'origin' => 1, ], [ 'rate' => 4, 'origin' => 2, ], [ 'rate' => 5, 'origin' => 3, ], ], [ [ 'rate' => 2, 'origin' => 1, ], [ 'rate' => 3, 'origin' => 2, ], [ 'rate' => 5, 'origin' => 3, ], ], [ [ 'rate' => 2, 'origin' => 1, ], [ 'rate' => 4, 'origin' => 2, ], [ 'rate' => 5, 'origin' => 3, ], ], ]; $expected = []; $iteratorCount = 0; foreach ($data as $key => $block) { $data[$key]['iterator'] = 0; $iteratorCount++; } $tempCounter = 0; while ($tempCounter < $iteratorCount) { $temp = []; $tempCounter = 0; foreach ($data as $key => $block) { $temp[] = ['rate' => $block['rates'][$block['iterator']],'origin' => $block['origin']]; } $expected[] = $temp; foreach ($data as $key => $block) { if ($block['iterator'] + 1 < count($block['rates'])) { $data[$key]['iterator']++; break; } else { $data[$key]['iterator'] = 0; $tempCounter++; } } } print_r($expected);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 12
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 12
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
2 jumps found. (Code = 44) Position 1 = 54, Position 2 = 15
Branch analysis from position: 54
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 77) Position 1 = 18, Position 2 = 29
Branch analysis from position: 18
2 jumps found. (Code = 78) Position 1 = 19, Position 2 = 29
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 29
2 jumps found. (Code = 77) Position 1 = 33, Position 2 = 51
Branch analysis from position: 33
2 jumps found. (Code = 78) Position 1 = 34, Position 2 = 51
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 46
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 51
Branch analysis from position: 51
2 jumps found. (Code = 44) Position 1 = 54, Position 2 = 15
Branch analysis from position: 54
Branch analysis from position: 15
Branch analysis from position: 46
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
Branch analysis from position: 51
Branch analysis from position: 51
Branch analysis from position: 29
Branch analysis from position: 12
filename:       /in/viVlL
function name:  (null)
number of ops:  58
compiled vars:  !0 = $data, !1 = $expected1, !2 = $expected, !3 = $iteratorCount, !4 = $block, !5 = $key, !6 = $tempCounter, !7 = $temp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   20     1        ASSIGN                                                   !1, <array>
   78     2        ASSIGN                                                   !2, <array>
   79     3        ASSIGN                                                   !3, 0
   80     4      > FE_RESET_R                                       $12     !0, ->12
          5    > > FE_FETCH_R                                       ~13     $12, !4, ->12
          6    >   ASSIGN                                                   !5, ~13
   81     7        FETCH_DIM_W                                      $15     !0, !5
          8        ASSIGN_DIM                                               $15, 'iterator'
          9        OP_DATA                                                  0
   82    10        PRE_INC                                                  !3
   80    11      > JMP                                                      ->5
         12    >   FE_FREE                                                  $12
   84    13        ASSIGN                                                   !6, 0
   85    14      > JMP                                                      ->52
   86    15    >   ASSIGN                                                   !7, <array>
   87    16        ASSIGN                                                   !6, 0
   88    17      > FE_RESET_R                                       $21     !0, ->29
         18    > > FE_FETCH_R                                       ~22     $21, !4, ->29
         19    >   ASSIGN                                                   !5, ~22
   89    20        FETCH_DIM_R                                      ~26     !4, 'iterator'
         21        FETCH_DIM_R                                      ~25     !4, 'rates'
         22        FETCH_DIM_R                                      ~27     ~25, ~26
         23        INIT_ARRAY                                       ~28     ~27, 'rate'
         24        FETCH_DIM_R                                      ~29     !4, 'origin'
         25        ADD_ARRAY_ELEMENT                                ~28     ~29, 'origin'
         26        ASSIGN_DIM                                               !7
         27        OP_DATA                                                  ~28
   88    28      > JMP                                                      ->18
         29    >   FE_FREE                                                  $21
   91    30        ASSIGN_DIM                                               !2
         31        OP_DATA                                                  !7
   92    32      > FE_RESET_R                                       $31     !0, ->51
         33    > > FE_FETCH_R                                       ~32     $31, !4, ->51
         34    >   ASSIGN                                                   !5, ~32
   93    35        FETCH_DIM_R                                      ~34     !4, 'iterator'
         36        ADD                                              ~35     ~34, 1
         37        FETCH_DIM_R                                      ~36     !4, 'rates'
         38        COUNT                                            ~37     ~36
         39        IS_SMALLER                                               ~35, ~37
         40      > JMPZ                                                     ~38, ->46
   94    41    >   FETCH_DIM_RW                                     $39     !0, !5
         42        FETCH_DIM_RW                                     $40     $39, 'iterator'
         43        PRE_INC                                                  $40
   95    44      > JMP                                                      ->51
         45*       JMP                                                      ->50
   97    46    >   FETCH_DIM_W                                      $42     !0, !5
         47        ASSIGN_DIM                                               $42, 'iterator'
         48        OP_DATA                                                  0
   98    49        PRE_INC                                                  !6
   92    50      > JMP                                                      ->33
         51    >   FE_FREE                                                  $31
   85    52    >   IS_SMALLER                                               !6, !3
         53      > JMPNZ                                                    ~45, ->15
  103    54    >   INIT_FCALL                                               'print_r'
         55        SEND_VAR                                                 !2
         56        DO_ICALL                                                 
         57      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
172.17 ms | 1400 KiB | 15 Q