3v4l.org

run code in 300+ PHP versions simultaneously
<?php function cartesian($input) { $result = array(); while (list($key, $values) = each($input)) { if (empty($values)) { continue; } if (empty($result)) { foreach($values as $value) { $result[] = array($key => $value); } } else { $append = array(); foreach($result as &$product) { $product[$key] = array_shift($values); $copy = $product; foreach($values as $item) { $copy[$key] = $item; $append[] = $copy; } array_unshift($values, $product[$key]); } $result = array_merge($result, $append); } } return $result; } $data = array ( 'machineAttribute' => array ( 'TTT 1000 S', 'TTT 1100 S' ), 'technicalAttribute' => array ( 'Certificate', 'Software' ), 'languageAttribute' => array ( 'English', 'Spanish', ) ); $combos = cartesian($data); $final_result = []; foreach($combos as $combo){ $final_result[] = implode('/',$combo); } print_r($final_result);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 15
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 15
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/Zh6Ws
function name:  (null)
number of ops:  20
compiled vars:  !0 = $data, !1 = $combos, !2 = $final_result, !3 = $combo
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   ASSIGN                                                   !0, <array>
   58     1        INIT_FCALL                                               'cartesian'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0  $5      
          4        ASSIGN                                                   !1, $5
   59     5        ASSIGN                                                   !2, <array>
   60     6      > FE_RESET_R                                       $8      !1, ->15
          7    > > FE_FETCH_R                                               $8, !3, ->15
   61     8    >   INIT_FCALL                                               'implode'
          9        SEND_VAL                                                 '%2F'
         10        SEND_VAR                                                 !3
         11        DO_ICALL                                         $10     
         12        ASSIGN_DIM                                               !2
         13        OP_DATA                                                  $10
   60    14      > JMP                                                      ->7
         15    >   FE_FREE                                                  $8
   63    16        INIT_FCALL                                               'print_r'
         17        SEND_VAR                                                 !2
         18        DO_ICALL                                                 
         19      > RETURN                                                   1

Function cartesian:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
2 jumps found. (Code = 44) Position 1 = 53, Position 2 = 3
Branch analysis from position: 53
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 16
Branch analysis from position: 8
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 14
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 14
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
Branch analysis from position: 14
Branch analysis from position: 16
2 jumps found. (Code = 125) Position 1 = 18, Position 2 = 39
Branch analysis from position: 18
2 jumps found. (Code = 126) Position 1 = 19, Position 2 = 39
Branch analysis from position: 19
2 jumps found. (Code = 77) Position 1 = 26, Position 2 = 32
Branch analysis from position: 26
2 jumps found. (Code = 78) Position 1 = 27, Position 2 = 32
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 32
Branch analysis from position: 39
2 jumps found. (Code = 44) Position 1 = 53, Position 2 = 3
Branch analysis from position: 53
Branch analysis from position: 3
Branch analysis from position: 39
filename:       /in/Zh6Ws
function name:  cartesian
number of ops:  55
compiled vars:  !0 = $input, !1 = $result, !2 = $values, !3 = $value, !4 = $key, !5 = $append, !6 = $product, !7 = $copy, !8 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
    3     1        ASSIGN                                                   !1, <array>
    5     2      > JMP                                                      ->45
    7     3    >   ISSET_ISEMPTY_CV                                         !2
          4      > JMPZ                                                     ~10, ->6
    8     5    > > JMP                                                      ->45
   10     6    >   ISSET_ISEMPTY_CV                                         !1
          7      > JMPZ                                                     ~11, ->16
   11     8    > > FE_RESET_R                                       $12     !2, ->14
          9    > > FE_FETCH_R                                               $12, !3, ->14
   12    10    >   INIT_ARRAY                                       ~14     !3, !4
         11        ASSIGN_DIM                                               !1
         12        OP_DATA                                                  ~14
   11    13      > JMP                                                      ->9
         14    >   FE_FREE                                                  $12
   10    15      > JMP                                                      ->45
   17    16    >   ASSIGN                                                   !5, <array>
   19    17      > FE_RESET_RW                                      $16     !1, ->39
         18    > > FE_FETCH_RW                                              $16, !6, ->39
   21    19    >   INIT_FCALL                                               'array_shift'
         20        SEND_REF                                                 !2
         21        DO_ICALL                                         $18     
         22        ASSIGN_DIM                                               !6, !4
         23        OP_DATA                                                  $18
   22    24        ASSIGN                                                   !7, !6
   23    25      > FE_RESET_R                                       $20     !2, ->32
         26    > > FE_FETCH_R                                               $20, !8, ->32
   24    27    >   ASSIGN_DIM                                               !7, !4
         28        OP_DATA                                                  !8
   25    29        ASSIGN_DIM                                               !5
         30        OP_DATA                                                  !7
   23    31      > JMP                                                      ->26
         32    >   FE_FREE                                                  $20
   27    33        INIT_FCALL                                               'array_unshift'
         34        SEND_REF                                                 !2
         35        FETCH_DIM_R                                      ~23     !6, !4
         36        SEND_VAL                                                 ~23
         37        DO_ICALL                                                 
   19    38      > JMP                                                      ->18
         39    >   FE_FREE                                                  $16
   29    40        INIT_FCALL                                               'array_merge'
         41        SEND_VAR                                                 !1
         42        SEND_VAR                                                 !5
         43        DO_ICALL                                         $25     
         44        ASSIGN                                                   !1, $25
    5    45    >   INIT_FCALL_BY_NAME                                       'each'
         46        SEND_VAR_EX                                              !0
         47        DO_FCALL                                      0  $27     
         48        FETCH_LIST_R                                     $28     $27, 0
         49        ASSIGN                                                   !4, $28
         50        FETCH_LIST_R                                     $30     $27, 1
         51        ASSIGN                                                   !2, $30
         52      > JMPNZ                                                    $27, ->3
   33    53    > > RETURN                                                   !1
   34    54*     > RETURN                                                   null

End of function cartesian

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
138.75 ms | 1013 KiB | 19 Q