3v4l.org

run code in 300+ PHP versions simultaneously
<?php // generic function to generate cross product of multi-dimensional array function arrayCrossProduct( array $data ) { $rowCount = 1; foreach( $data as &$datum ) { $datum = (array) $datum; $rowCount *= count( $datum ); reset( $datum ); // reset the array, just to be sure } unset( $datum ); // drop the reference, just to be safe $result = array(); while( $rowCount-- > 0 ) { $row = array(); foreach( $data as &$datum ) { $row[] = current( $datum ); } $result[] = $row; foreach( $data as &$datum ) { next( $datum ); if( !is_null( key( $datum ) ) ) { break; } reset( $datum ); } } return $result; } // example data $data = array( 'name' => 'Mike', 'country' => 'UK', 'prefers' => array( 'coffee', 'milk', 'bananas' ), 'age' => 25, 'parents' => array( 'John', 'Janine' ), 'children' => array( 'Jack', 'Jill', 'Joline' ) ); $i = 1; foreach( arrayCrossProduct( $data ) as $row ) { echo str_pad( (string) $i++, 2, " ", STR_PAD_LEFT ) . '. ' . implode( ', ', $row ) . PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 24
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 24
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/4EtIF
function name:  (null)
number of ops:  26
compiled vars:  !0 = $data, !1 = $i, !2 = $row
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   ASSIGN                                                   !0, <array>
   44     1        ASSIGN                                                   !1, 1
   45     2        INIT_FCALL                                               'arraycrossproduct'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $5      
          5      > FE_RESET_R                                       $6      $5, ->24
          6    > > FE_FETCH_R                                               $6, !2, ->24
   46     7    >   INIT_FCALL                                               'str_pad'
          8        POST_INC                                         ~7      !1
          9        CAST                                          6  ~8      ~7
         10        SEND_VAL                                                 ~8
         11        SEND_VAL                                                 2
         12        SEND_VAL                                                 '+'
         13        SEND_VAL                                                 0
         14        DO_ICALL                                         $9      
         15        CONCAT                                           ~10     $9, '.+'
         16        INIT_FCALL                                               'implode'
         17        SEND_VAL                                                 '%2C+'
         18        SEND_VAR                                                 !2
         19        DO_ICALL                                         $11     
         20        CONCAT                                           ~12     ~10, $11
         21        CONCAT                                           ~13     ~12, '%0A'
         22        ECHO                                                     ~13
   45    23      > JMP                                                      ->6
         24    >   FE_FREE                                                  $6
   47    25      > RETURN                                                   1

Function arraycrossproduct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 3, Position 2 = 12
Branch analysis from position: 3
2 jumps found. (Code = 126) Position 1 = 4, Position 2 = 12
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
2 jumps found. (Code = 44) Position 1 = 48, Position 2 = 16
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
2 jumps found. (Code = 125) Position 1 = 18, Position 2 = 25
Branch analysis from position: 18
2 jumps found. (Code = 126) Position 1 = 19, Position 2 = 25
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 25
2 jumps found. (Code = 125) Position 1 = 29, Position 2 = 44
Branch analysis from position: 29
2 jumps found. (Code = 126) Position 1 = 30, Position 2 = 44
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 40
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
2 jumps found. (Code = 44) Position 1 = 48, Position 2 = 16
Branch analysis from position: 48
Branch analysis from position: 16
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
Branch analysis from position: 44
Branch analysis from position: 44
Branch analysis from position: 25
Branch analysis from position: 12
filename:       /in/4EtIF
function name:  arrayCrossProduct
number of ops:  50
compiled vars:  !0 = $data, !1 = $rowCount, !2 = $datum, !3 = $result, !4 = $row
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
    5     1        ASSIGN                                                   !1, 1
    7     2      > FE_RESET_RW                                      $6      !0, ->12
          3    > > FE_FETCH_RW                                              $6, !2, ->12
    8     4    >   CAST                                          7  ~7      !2
          5        ASSIGN                                                   !2, ~7
    9     6        COUNT                                            ~9      !2
          7        ASSIGN_OP                                     3          !1, ~9
   10     8        INIT_FCALL                                               'reset'
          9        SEND_REF                                                 !2
         10        DO_ICALL                                                 
    7    11      > JMP                                                      ->3
         12    >   FE_FREE                                                  $6
   12    13        UNSET_CV                                                 !2
   14    14        ASSIGN                                                   !3, <array>
   15    15      > JMP                                                      ->45
   16    16    >   ASSIGN                                                   !4, <array>
   17    17      > FE_RESET_RW                                      $14     !0, ->25
         18    > > FE_FETCH_RW                                              $14, !2, ->25
   18    19    >   INIT_FCALL                                               'current'
         20        SEND_VAR                                                 !2
         21        DO_ICALL                                         $16     
         22        ASSIGN_DIM                                               !4
         23        OP_DATA                                                  $16
   17    24      > JMP                                                      ->18
         25    >   FE_FREE                                                  $14
   20    26        ASSIGN_DIM                                               !3
         27        OP_DATA                                                  !4
   22    28      > FE_RESET_RW                                      $18     !0, ->44
         29    > > FE_FETCH_RW                                              $18, !2, ->44
   23    30    >   INIT_FCALL                                               'next'
         31        SEND_REF                                                 !2
         32        DO_ICALL                                                 
   24    33        INIT_FCALL                                               'key'
         34        SEND_VAR                                                 !2
         35        DO_ICALL                                         $20     
         36        TYPE_CHECK                                    2  ~21     $20
         37        BOOL_NOT                                         ~22     ~21
         38      > JMPZ                                                     ~22, ->40
   25    39    > > JMP                                                      ->44
   27    40    >   INIT_FCALL                                               'reset'
         41        SEND_REF                                                 !2
         42        DO_ICALL                                                 
   22    43      > JMP                                                      ->29
         44    >   FE_FREE                                                  $18
   15    45    >   POST_DEC                                         ~24     !1
         46        IS_SMALLER                                               0, ~24
         47      > JMPNZ                                                    ~25, ->16
   31    48    > > RETURN                                                   !3
   32    49*     > RETURN                                                   null

End of function arraycrossproduct

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.04 ms | 1411 KiB | 26 Q