3v4l.org

run code in 300+ PHP versions simultaneously
<?php function nextAssoc($assoc) { if(false !== ($pos = strrpos($assoc, '01'))) { $assoc[$pos] = '1'; $assoc[$pos+1] = '0'; return substr($assoc, 0, $pos+2). str_repeat('0', substr_count(substr($assoc, $pos+2), '0')). str_repeat('1', substr_count(substr($assoc, $pos+2), '1')); } return false; } function getAssoc(array $data, $count=2, $split=true) { if(count($data)<$count) { return null; } $assoc = str_repeat('0', count($data)-$count).str_repeat('1', $count); $result = []; do { $result[]=array_intersect_key($data, array_filter(str_split($assoc))); } while($assoc=nextAssoc($assoc)); return $split?array_chunk($result, (int)(count($data)/$count)):$result; } $data = [1,2,3,4,5,6]; print_r(getAssoc($data, 2)); print_r(getAssoc($data, 3));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vJJJn
function name:  (null)
number of ops:  16
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   ASSIGN                                                   !0, <array>
   34     1        INIT_FCALL                                               'print_r'
          2        INIT_FCALL                                               'getassoc'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 2
          5        DO_FCALL                                      0  $2      
          6        SEND_VAR                                                 $2
          7        DO_ICALL                                                 
   35     8        INIT_FCALL                                               'print_r'
          9        INIT_FCALL                                               'getassoc'
         10        SEND_VAR                                                 !0
         11        SEND_VAL                                                 3
         12        DO_FCALL                                      0  $4      
         13        SEND_VAR                                                 $4
         14        DO_ICALL                                                 
         15      > RETURN                                                   1

Function nextassoc:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 48
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vJJJn
function name:  nextAssoc
number of ops:  50
compiled vars:  !0 = $assoc, !1 = $pos
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        INIT_FCALL                                               'strrpos'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 '01'
          4        DO_ICALL                                         $2      
          5        ASSIGN                                           ~3      !1, $2
          6        TYPE_CHECK                                  1018          ~3
          7      > JMPZ                                                     ~4, ->48
    7     8    >   ASSIGN_DIM                                               !0, !1
          9        OP_DATA                                                  '1'
    8    10        ADD                                              ~6      !1, 1
         11        ASSIGN_DIM                                               !0, ~6
         12        OP_DATA                                                  '0'
    9    13        INIT_FCALL                                               'substr'
         14        SEND_VAR                                                 !0
         15        SEND_VAL                                                 0
         16        ADD                                              ~8      !1, 2
         17        SEND_VAL                                                 ~8
         18        DO_ICALL                                         $9      
   10    19        INIT_FCALL                                               'str_repeat'
         20        SEND_VAL                                                 '0'
         21        INIT_FCALL                                               'substr_count'
         22        INIT_FCALL                                               'substr'
         23        SEND_VAR                                                 !0
         24        ADD                                              ~10     !1, 2
         25        SEND_VAL                                                 ~10
         26        DO_ICALL                                         $11     
         27        SEND_VAR                                                 $11
         28        SEND_VAL                                                 '0'
         29        DO_ICALL                                         $12     
         30        SEND_VAR                                                 $12
         31        DO_ICALL                                         $13     
         32        CONCAT                                           ~14     $9, $13
   11    33        INIT_FCALL                                               'str_repeat'
         34        SEND_VAL                                                 '1'
         35        INIT_FCALL                                               'substr_count'
         36        INIT_FCALL                                               'substr'
         37        SEND_VAR                                                 !0
         38        ADD                                              ~15     !1, 2
         39        SEND_VAL                                                 ~15
         40        DO_ICALL                                         $16     
         41        SEND_VAR                                                 $16
         42        SEND_VAL                                                 '1'
         43        DO_ICALL                                         $17     
         44        SEND_VAR                                                 $17
         45        DO_ICALL                                         $18     
         46        CONCAT                                           ~19     ~14, $18
         47      > RETURN                                                   ~19
   13    48    > > RETURN                                                   <false>
   14    49*     > RETURN                                                   null

End of function nextassoc

Function getassoc:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 37, Position 2 = 20
Branch analysis from position: 37
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 47
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/vJJJn
function name:  getAssoc
number of ops:  50
compiled vars:  !0 = $data, !1 = $count, !2 = $split, !3 = $assoc, !4 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      2
          2        RECV_INIT                                        !2      <true>
   18     3        COUNT                                            ~5      !0
          4        IS_SMALLER                                               ~5, !1
          5      > JMPZ                                                     ~6, ->7
   20     6    > > RETURN                                                   null
   22     7    >   INIT_FCALL                                               'str_repeat'
          8        SEND_VAL                                                 '0'
          9        COUNT                                            ~7      !0
         10        SUB                                              ~8      ~7, !1
         11        SEND_VAL                                                 ~8
         12        DO_ICALL                                         $9      
         13        INIT_FCALL                                               'str_repeat'
         14        SEND_VAL                                                 '1'
         15        SEND_VAR                                                 !1
         16        DO_ICALL                                         $10     
         17        CONCAT                                           ~11     $9, $10
         18        ASSIGN                                                   !3, ~11
   23    19        ASSIGN                                                   !4, <array>
   26    20    >   INIT_FCALL                                               'array_intersect_key'
         21        SEND_VAR                                                 !0
         22        INIT_FCALL                                               'array_filter'
         23        INIT_FCALL                                               'str_split'
         24        SEND_VAR                                                 !3
         25        DO_ICALL                                         $15     
         26        SEND_VAR                                                 $15
         27        DO_ICALL                                         $16     
         28        SEND_VAR                                                 $16
         29        DO_ICALL                                         $17     
         30        ASSIGN_DIM                                               !4
         31        OP_DATA                                                  $17
   28    32        INIT_FCALL                                               'nextassoc'
         33        SEND_VAR                                                 !3
         34        DO_FCALL                                      0  $18     
         35        ASSIGN                                           ~19     !3, $18
         36      > JMPNZ                                                    ~19, ->20
   29    37    > > JMPZ                                                     !2, ->47
         38    >   INIT_FCALL                                               'array_chunk'
         39        SEND_VAR                                                 !4
         40        COUNT                                            ~20     !0
         41        DIV                                              ~21     ~20, !1
         42        CAST                                          4  ~22     ~21
         43        SEND_VAL                                                 ~22
         44        DO_ICALL                                         $23     
         45        QM_ASSIGN                                        ~24     $23
         46      > JMP                                                      ->48
         47    >   QM_ASSIGN                                        ~24     !4
         48    > > RETURN                                                   ~24
   30    49*     > RETURN                                                   null

End of function getassoc

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
167.48 ms | 1407 KiB | 34 Q