3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = ['a', 'b', 'c', 'd','e','f','g']; function combinations($array, $k) { if ($k === 0) { return [[]]; } if (count($array) === $k) { return [$array]; } if (count($array) < $k) { return []; } $results = []; list($firstElem, $restArray) = [array_slice($array, 0, 1), array_slice($array, 1)]; foreach (combinations($restArray, $k - 1) as $combination) { $results[] = array_merge($firstElem, $combination); } foreach (combinations($restArray, $k) as $combination) { $results[] = $combination; } return $results; } $combinations = array_map('implode', combinations($array, 2)); print_r($combinations); ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/lgpeD
function name:  (null)
number of ops:  14
compiled vars:  !0 = $array, !1 = $combinations
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   33     1        INIT_FCALL                                               'array_map'
          2        SEND_VAL                                                 'implode'
          3        INIT_FCALL                                               'combinations'
          4        SEND_VAR                                                 !0
          5        SEND_VAL                                                 2
          6        DO_FCALL                                      0  $3      
          7        SEND_VAR                                                 $3
          8        DO_ICALL                                         $4      
          9        ASSIGN                                                   !1, $4
   34    10        INIT_FCALL                                               'print_r'
         11        SEND_VAR                                                 !1
         12        DO_ICALL                                                 
   36    13      > RETURN                                                   1

Function combinations:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 14
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
2 jumps found. (Code = 77) Position 1 = 37, Position 2 = 45
Branch analysis from position: 37
2 jumps found. (Code = 78) Position 1 = 38, Position 2 = 45
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
Branch analysis from position: 45
2 jumps found. (Code = 77) Position 1 = 51, Position 2 = 55
Branch analysis from position: 51
2 jumps found. (Code = 78) Position 1 = 52, Position 2 = 55
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 51
Branch analysis from position: 51
Branch analysis from position: 55
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 55
Branch analysis from position: 45
filename:       /in/lgpeD
function name:  combinations
number of ops:  58
compiled vars:  !0 = $array, !1 = $k, !2 = $results, !3 = $firstElem, !4 = $restArray, !5 = $combination
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    6     2        IS_IDENTICAL                                             !1, 0
          3      > JMPZ                                                     ~6, ->5
    7     4    > > RETURN                                                   <array>
   10     5    >   COUNT                                            ~7      !0
          6        IS_IDENTICAL                                             !1, ~7
          7      > JMPZ                                                     ~8, ->10
   11     8    >   INIT_ARRAY                                       ~9      !0
          9      > RETURN                                                   ~9
   14    10    >   COUNT                                            ~10     !0
         11        IS_SMALLER                                               ~10, !1
         12      > JMPZ                                                     ~11, ->14
   15    13    > > RETURN                                                   <array>
   18    14    >   ASSIGN                                                   !2, <array>
   20    15        INIT_FCALL                                               'array_slice'
         16        SEND_VAR                                                 !0
         17        SEND_VAL                                                 0
         18        SEND_VAL                                                 1
         19        DO_ICALL                                         $13     
         20        INIT_ARRAY                                       ~14     $13
         21        INIT_FCALL                                               'array_slice'
         22        SEND_VAR                                                 !0
         23        SEND_VAL                                                 1
         24        DO_ICALL                                         $15     
         25        ADD_ARRAY_ELEMENT                                ~14     $15
         26        FETCH_LIST_R                                     $16     ~14, 0
         27        ASSIGN                                                   !3, $16
         28        FETCH_LIST_R                                     $18     ~14, 1
         29        ASSIGN                                                   !4, $18
         30        FREE                                                     ~14
   22    31        INIT_FCALL_BY_NAME                                       'combinations'
         32        SEND_VAR_EX                                              !4
         33        SUB                                              ~20     !1, 1
         34        SEND_VAL_EX                                              ~20
         35        DO_FCALL                                      0  $21     
         36      > FE_RESET_R                                       $22     $21, ->45
         37    > > FE_FETCH_R                                               $22, !5, ->45
   23    38    >   INIT_FCALL                                               'array_merge'
         39        SEND_VAR                                                 !3
         40        SEND_VAR                                                 !5
         41        DO_ICALL                                         $24     
         42        ASSIGN_DIM                                               !2
         43        OP_DATA                                                  $24
   22    44      > JMP                                                      ->37
         45    >   FE_FREE                                                  $22
   26    46        INIT_FCALL_BY_NAME                                       'combinations'
         47        SEND_VAR_EX                                              !4
         48        SEND_VAR_EX                                              !1
         49        DO_FCALL                                      0  $25     
         50      > FE_RESET_R                                       $26     $25, ->55
         51    > > FE_FETCH_R                                               $26, !5, ->55
   27    52    >   ASSIGN_DIM                                               !2
         53        OP_DATA                                                  !5
   26    54      > JMP                                                      ->51
         55    >   FE_FREE                                                  $26
   30    56      > RETURN                                                   !2
   31    57*     > RETURN                                                   null

End of function combinations

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.26 ms | 1021 KiB | 18 Q