3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = array( array('optional' => 0, 2, 9), array('optional' => 0, 3, 4),/* array('optional' => 0, 6, 7, 8),*/ array('optional' => 1, 6,),/* array('optional' => 1, 6, 7, 8, 5, 9), array('optional' => 1, 6, 7, 8, 10, 11, 12)*/ ); function generateCombinations(array $array) { foreach (array_pop($array) as $value) { $option = $value["optional"]; if (count($array)) { foreach (generateCombinations($array) as $combination) { yield array_merge([$value], $combination); }; } else { yield [$value]; if($option == 1) yield []; } } } foreach(generateCombinations($arr) as $combination){ echo implode(",", $combiantion) . "\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 13
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 13
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/gNBsJ
function name:  (null)
number of ops:  15
compiled vars:  !0 = $arr, !1 = $combination, !2 = $combiantion
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   29     1        INIT_FCALL                                               'generatecombinations'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $4      
          4      > FE_RESET_R                                       $5      $4, ->13
          5    > > FE_FETCH_R                                               $5, !1, ->13
   30     6    >   INIT_FCALL                                               'implode'
          7        SEND_VAL                                                 '%2C'
          8        SEND_VAR                                                 !2
          9        DO_ICALL                                         $6      
         10        CONCAT                                           ~7      $6, '%0A'
         11        ECHO                                                     ~7
   29    12      > JMP                                                      ->5
         13    >   FE_FREE                                                  $5
   31    14      > RETURN                                                   1

Function generatecombinations:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 31
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 31
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 25
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 23
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 23
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 23
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 30
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 30
Branch analysis from position: 31
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 31
filename:       /in/gNBsJ
function name:  generateCombinations
number of ops:  33
compiled vars:  !0 = $array, !1 = $value, !2 = $option, !3 = $combination
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        GENERATOR_CREATE                                         
   14     2        INIT_FCALL                                               'array_pop'
          3        SEND_REF                                                 !0
          4        DO_ICALL                                         $4      
          5      > FE_RESET_R                                       $5      $4, ->31
          6    > > FE_FETCH_R                                               $5, !1, ->31
   15     7    >   FETCH_DIM_R                                      ~6      !1, 'optional'
          8        ASSIGN                                                   !2, ~6
   16     9        COUNT                                            ~8      !0
         10      > JMPZ                                                     ~8, ->25
   17    11    >   INIT_FCALL_BY_NAME                                       'generateCombinations'
         12        SEND_VAR_EX                                              !0
         13        DO_FCALL                                      0  $9      
         14      > FE_RESET_R                                       $10     $9, ->23
         15    > > FE_FETCH_R                                               $10, !3, ->23
   18    16    >   INIT_FCALL                                               'array_merge'
         17        INIT_ARRAY                                       ~11     !1
         18        SEND_VAL                                                 ~11
         19        SEND_VAR                                                 !3
         20        DO_ICALL                                         $12     
         21        YIELD                                                    $12
   17    22      > JMP                                                      ->15
         23    >   FE_FREE                                                  $10
   16    24      > JMP                                                      ->30
   21    25    >   INIT_ARRAY                                       ~14     !1
         26        YIELD                                                    ~14
   22    27        IS_EQUAL                                                 !2, 1
         28      > JMPZ                                                     ~16, ->30
   23    29    >   YIELD                                                    <array>
   14    30    > > JMP                                                      ->6
         31    >   FE_FREE                                                  $5
   26    32      > GENERATOR_RETURN                                         

End of function generatecombinations

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.55 ms | 1020 KiB | 17 Q