3v4l.org

run code in 300+ PHP versions simultaneously
<?php function array_transpose_basic(array|object $object_or_array): array|object { $levelOneType = gettype($object_or_array); if (!in_array($levelOneType, ['array', 'object'])) { throw new Exception("Error: array_transpose() expects parameter 1 to be an array or object, $levelOneType given"); } $result = []; foreach ($object_or_array as $rowKey => $row) { $levelTwoType = gettype($row); if (!in_array($levelTwoType, ['array', 'object'])) { throw new Exception("Error: array_transpose() expects parameter 1 to contain rows of arrays or objects, $levelTwoType given"); } foreach ($row as $columnKey => $value) { $result[$columnKey][$rowKey] = $value; } } return $result; } function array_transpose_types(array|object $object_or_array): array|object { $levelOneType = gettype($object_or_array); if (!in_array($levelOneType, ['array', 'object'])) { throw new Exception("Fatal error: Uncaught TypeError: array_transpose(): Argument #1 ($object_or_array) must be of type object|array, $levelOneType given"); } foreach ($object_or_array as $rowKey => $row) { $levelTwoType = gettype($row); if (!in_array($levelTwoType, ['array', 'object'])) { throw new Exception("Fatal error: Uncaught TypeError: array_transpose(): Argument #1 ($object_or_array) must contain rows of type object|array, $levelTwoType given"); } $result ??= ($levelTwoType === 'array' ? [] : (object)[]); foreach ($row as $columnKey => $value) { if ($levelTwoType === 'array') { if ($levelOneType === 'array') { $result[$columnKey][$rowKey] = $value; } else { $result[$columnKey]->$rowKey = $value; } } else { if (!property_exists($result, $columnKey)) { $result->$columnKey = ($levelOneType === 'array' ? [] : (object)[]); } if ($levelOneType === 'array') { $result->{$columnKey}[$rowKey] = $value; } else { $result->{$columnKey}->$rowKey = $value; } } } } return $result ?? ($levelOneType === 'array' ? [] : (object)[]); } function array_transpose_splat(array|object $object_or_array): array { if (!is_array($object_or_array)) { return ["must be array"]; } if (!$object_or_array) { return ["must not be empty"]; } foreach ($object_or_array as $key => $value) { if (!ctype_digit((string)$key)) { return ["may only contain numeric first level keys"]; } if (!is_array($value)) { return ["may only contain arrays in second level"]; } } return array_map(null, ...$object_or_array); } $test = (object) []; echo var_export(array_transpose_basic($test), true) . "\n---\n"; echo var_export(array_transpose_types($test), true) . "\n---\n"; echo var_export(array_transpose_splat($test), true);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MZv9c
function name:  (null)
number of ops:  29
compiled vars:  !0 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   CAST                                          8  ~1      <array>
          1        ASSIGN                                                   !0, ~1
   73     2        INIT_FCALL                                               'var_export'
          3        INIT_FCALL                                               'array_transpose_basic'
          4        SEND_VAR                                                 !0
          5        DO_FCALL                                      0  $3      
          6        SEND_VAR                                                 $3
          7        SEND_VAL                                                 <true>
          8        DO_ICALL                                         $4      
          9        CONCAT                                           ~5      $4, '%0A---%0A'
         10        ECHO                                                     ~5
   74    11        INIT_FCALL                                               'var_export'
         12        INIT_FCALL                                               'array_transpose_types'
         13        SEND_VAR                                                 !0
         14        DO_FCALL                                      0  $6      
         15        SEND_VAR                                                 $6
         16        SEND_VAL                                                 <true>
         17        DO_ICALL                                         $7      
         18        CONCAT                                           ~8      $7, '%0A---%0A'
         19        ECHO                                                     ~8
   75    20        INIT_FCALL                                               'var_export'
         21        INIT_FCALL                                               'array_transpose_splat'
         22        SEND_VAR                                                 !0
         23        DO_FCALL                                      0  $9      
         24        SEND_VAR                                                 $9
         25        SEND_VAL                                                 <true>
         26        DO_ICALL                                         $10     
         27        ECHO                                                     $10
         28      > RETURN                                                   1

Function array_transpose_basic:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 13
Branch analysis from position: 6
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 38
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 38
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 29
Branch analysis from position: 22
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 29
2 jumps found. (Code = 77) Position 1 = 30, Position 2 = 36
Branch analysis from position: 30
2 jumps found. (Code = 78) Position 1 = 31, Position 2 = 36
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 36
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
filename:       /in/MZv9c
function name:  array_transpose_basic
number of ops:  43
compiled vars:  !0 = $object_or_array, !1 = $levelOneType, !2 = $result, !3 = $row, !4 = $rowKey, !5 = $levelTwoType, !6 = $value, !7 = $columnKey
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        GET_TYPE                                         ~8      !0
          2        ASSIGN                                                   !1, ~8
    5     3        IN_ARRAY                                         ~10     !1, <array>
          4        BOOL_NOT                                         ~11     ~10
          5      > JMPZ                                                     ~11, ->13
    6     6    >   NEW                                              $12     'Exception'
          7        ROPE_INIT                                     3  ~14     'Error%3A+array_transpose%28%29+expects+parameter+1+to+be+an+array+or+object%2C+'
          8        ROPE_ADD                                      1  ~14     ~14, !1
          9        ROPE_END                                      2  ~13     ~14, '+given'
         10        SEND_VAL_EX                                              ~13
         11        DO_FCALL                                      0          
         12      > THROW                                         0          $12
    8    13    >   ASSIGN                                                   !2, <array>
    9    14      > FE_RESET_R                                       $18     !0, ->38
         15    > > FE_FETCH_R                                       ~19     $18, !3, ->38
         16    >   ASSIGN                                                   !4, ~19
   10    17        GET_TYPE                                         ~21     !3
         18        ASSIGN                                                   !5, ~21
   11    19        IN_ARRAY                                         ~23     !5, <array>
         20        BOOL_NOT                                         ~24     ~23
         21      > JMPZ                                                     ~24, ->29
   12    22    >   NEW                                              $25     'Exception'
         23        ROPE_INIT                                     3  ~27     'Error%3A+array_transpose%28%29+expects+parameter+1+to+contain+rows+of+arrays+or+objects%2C+'
         24        ROPE_ADD                                      1  ~27     ~27, !5
         25        ROPE_END                                      2  ~26     ~27, '+given'
         26        SEND_VAL_EX                                              ~26
         27        DO_FCALL                                      0          
         28      > THROW                                         0          $25
   14    29    > > FE_RESET_R                                       $30     !3, ->36
         30    > > FE_FETCH_R                                       ~31     $30, !6, ->36
         31    >   ASSIGN                                                   !7, ~31
   15    32        FETCH_DIM_W                                      $33     !2, !7
         33        ASSIGN_DIM                                               $33, !4
         34        OP_DATA                                                  !6
   14    35      > JMP                                                      ->30
         36    >   FE_FREE                                                  $30
    9    37      > JMP                                                      ->15
         38    >   FE_FREE                                                  $18
   18    39        VERIFY_RETURN_TYPE                                       !2
         40      > RETURN                                                   !2
   19    41*       VERIFY_RETURN_TYPE                                       
         42*     > RETURN                                                   null

End of function array_transpose_basic

Function array_transpose_types:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 15
Branch analysis from position: 6
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 83
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 83
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 32
Branch analysis from position: 23
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 37
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
2 jumps found. (Code = 77) Position 1 = 43, Position 2 = 81
Branch analysis from position: 43
2 jumps found. (Code = 78) Position 1 = 44, Position 2 = 81
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 57
Branch analysis from position: 47
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 53
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 56
Branch analysis from position: 56
1 jumps found. (Code = 42) Position 1 = 80
Branch analysis from position: 80
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 53
1 jumps found. (Code = 42) Position 1 = 80
Branch analysis from position: 80
Branch analysis from position: 57
2 jumps found. (Code = 43) Position 1 = 63, Position 2 = 71
Branch analysis from position: 63
2 jumps found. (Code = 43) Position 1 = 65, Position 2 = 67
Branch analysis from position: 65
1 jumps found. (Code = 42) Position 1 = 69
Branch analysis from position: 69
2 jumps found. (Code = 43) Position 1 = 73, Position 2 = 77
Branch analysis from position: 73
1 jumps found. (Code = 42) Position 1 = 80
Branch analysis from position: 80
Branch analysis from position: 77
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 67
2 jumps found. (Code = 43) Position 1 = 73, Position 2 = 77
Branch analysis from position: 73
Branch analysis from position: 77
Branch analysis from position: 71
Branch analysis from position: 81
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 81
Branch analysis from position: 37
2 jumps found. (Code = 77) Position 1 = 43, Position 2 = 81
Branch analysis from position: 43
Branch analysis from position: 81
Branch analysis from position: 83
2 jumps found. (Code = 43) Position 1 = 87, Position 2 = 89
Branch analysis from position: 87
1 jumps found. (Code = 42) Position 1 = 91
Branch analysis from position: 91
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 89
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 83
filename:       /in/MZv9c
function name:  array_transpose_types
number of ops:  96
compiled vars:  !0 = $object_or_array, !1 = $levelOneType, !2 = $row, !3 = $rowKey, !4 = $levelTwoType, !5 = $result, !6 = $value, !7 = $columnKey
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
   22     1        GET_TYPE                                         ~8      !0
          2        ASSIGN                                                   !1, ~8
   23     3        IN_ARRAY                                         ~10     !1, <array>
          4        BOOL_NOT                                         ~11     ~10
          5      > JMPZ                                                     ~11, ->15
   24     6    >   NEW                                              $12     'Exception'
          7        ROPE_INIT                                     5  ~14     'Fatal+error%3A+Uncaught+TypeError%3A+array_transpose%28%29%3A+Argument+%231+%28'
          8        ROPE_ADD                                      1  ~14     ~14, !0
          9        ROPE_ADD                                      2  ~14     ~14, '%29+must+be+of+type+object%7Carray%2C+'
         10        ROPE_ADD                                      3  ~14     ~14, !1
         11        ROPE_END                                      4  ~13     ~14, '+given'
         12        SEND_VAL_EX                                              ~13
         13        DO_FCALL                                      0          
         14      > THROW                                         0          $12
   26    15    > > FE_RESET_R                                       $18     !0, ->83
         16    > > FE_FETCH_R                                       ~19     $18, !2, ->83
         17    >   ASSIGN                                                   !3, ~19
   27    18        GET_TYPE                                         ~21     !2
         19        ASSIGN                                                   !4, ~21
   28    20        IN_ARRAY                                         ~23     !4, <array>
         21        BOOL_NOT                                         ~24     ~23
         22      > JMPZ                                                     ~24, ->32
   29    23    >   NEW                                              $25     'Exception'
         24        ROPE_INIT                                     5  ~27     'Fatal+error%3A+Uncaught+TypeError%3A+array_transpose%28%29%3A+Argument+%231+%28'
         25        ROPE_ADD                                      1  ~27     ~27, !0
         26        ROPE_ADD                                      2  ~27     ~27, '%29+must+contain+rows+of+type+object%7Carray%2C+'
         27        ROPE_ADD                                      3  ~27     ~27, !4
         28        ROPE_END                                      4  ~26     ~27, '+given'
         29        SEND_VAL_EX                                              ~26
         30        DO_FCALL                                      0          
         31      > THROW                                         0          $25
   31    32    >   COALESCE                                         ~31     !5
         33        IS_IDENTICAL                                             !4, 'array'
         34      > JMPZ                                                     ~32, ->37
         35    >   QM_ASSIGN                                        ~33     <array>
         36      > JMP                                                      ->39
         37    >   CAST                                          8  ~34     <array>
         38        QM_ASSIGN                                        ~33     ~34
         39    >   ASSIGN                                           ~35     !5, ~33
         40        QM_ASSIGN                                        ~31     ~35
         41        FREE                                                     ~31
   32    42      > FE_RESET_R                                       $36     !2, ->81
         43    > > FE_FETCH_R                                       ~37     $36, !6, ->81
         44    >   ASSIGN                                                   !7, ~37
   33    45        IS_IDENTICAL                                             !4, 'array'
         46      > JMPZ                                                     ~39, ->57
   34    47    >   IS_IDENTICAL                                             !1, 'array'
         48      > JMPZ                                                     ~40, ->53
   35    49    >   FETCH_DIM_W                                      $41     !5, !7
         50        ASSIGN_DIM                                               $41, !3
         51        OP_DATA                                                  !6
   34    52      > JMP                                                      ->56
   37    53    >   FETCH_DIM_W                                      $43     !5, !7
         54        ASSIGN_OBJ                                               $43, !3
         55        OP_DATA                                                  !6
   33    56    > > JMP                                                      ->80
   40    57    >   INIT_FCALL                                               'property_exists'
         58        SEND_VAR                                                 !5
         59        SEND_VAR                                                 !7
         60        DO_ICALL                                         $45     
         61        BOOL_NOT                                         ~46     $45
         62      > JMPZ                                                     ~46, ->71
   41    63    >   IS_IDENTICAL                                             !1, 'array'
         64      > JMPZ                                                     ~48, ->67
         65    >   QM_ASSIGN                                        ~49     <array>
         66      > JMP                                                      ->69
         67    >   CAST                                          8  ~50     <array>
         68        QM_ASSIGN                                        ~49     ~50
         69    >   ASSIGN_OBJ                                               !5, !7
         70        OP_DATA                                                  ~49
   43    71    >   IS_IDENTICAL                                             !1, 'array'
         72      > JMPZ                                                     ~51, ->77
   44    73    >   FETCH_OBJ_W                                      $52     !5, !7
         74        ASSIGN_DIM                                               $52, !3
         75        OP_DATA                                                  !6
   43    76      > JMP                                                      ->80
   46    77    >   FETCH_OBJ_W                                      $54     !5, !7
         78        ASSIGN_OBJ                                               $54, !3
         79        OP_DATA                                                  !6
   32    80    > > JMP                                                      ->43
         81    >   FE_FREE                                                  $36
   26    82      > JMP                                                      ->16
         83    >   FE_FREE                                                  $18
   51    84        COALESCE                                         ~56     !5
         85        IS_IDENTICAL                                             !1, 'array'
         86      > JMPZ                                                     ~57, ->89
         87    >   QM_ASSIGN                                        ~58     <array>
         88      > JMP                                                      ->91
         89    >   CAST                                          8  ~59     <array>
         90        QM_ASSIGN                                        ~58     ~59
         91    >   QM_ASSIGN                                        ~56     ~58
         92        VERIFY_RETURN_TYPE                                       ~56
         93      > RETURN                                                   ~56
   52    94*       VERIFY_RETURN_TYPE                                       
         95*     > RETURN                                                   null

End of function array_transpose_types

Function array_transpose_splat:
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 = 7, Position 2 = 8
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 25
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 25
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 19
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 24
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
filename:       /in/MZv9c
function name:  array_transpose_splat
number of ops:  35
compiled vars:  !0 = $object_or_array, !1 = $value, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   RECV                                             !0      
   55     1        TYPE_CHECK                                  128  ~3      !0
          2        BOOL_NOT                                         ~4      ~3
          3      > JMPZ                                                     ~4, ->5
   56     4    > > RETURN                                                   <array>
   58     5    >   BOOL_NOT                                         ~5      !0
          6      > JMPZ                                                     ~5, ->8
   59     7    > > RETURN                                                   <array>
   61     8    > > FE_RESET_R                                       $6      !0, ->25
          9    > > FE_FETCH_R                                       ~7      $6, !1, ->25
         10    >   ASSIGN                                                   !2, ~7
   62    11        INIT_FCALL                                               'ctype_digit'
         12        CAST                                          6  ~9      !2
         13        SEND_VAL                                                 ~9
         14        DO_ICALL                                         $10     
         15        BOOL_NOT                                         ~11     $10
         16      > JMPZ                                                     ~11, ->19
   63    17    >   FE_FREE                                                  $6
         18      > RETURN                                                   <array>
   65    19    >   TYPE_CHECK                                  128  ~12     !1
         20        BOOL_NOT                                         ~13     ~12
         21      > JMPZ                                                     ~13, ->24
   66    22    >   FE_FREE                                                  $6
         23      > RETURN                                                   <array>
   61    24    > > JMP                                                      ->9
         25    >   FE_FREE                                                  $6
   69    26        INIT_FCALL                                               'array_map'
         27        SEND_VAL                                                 null
         28        SEND_UNPACK                                              !0
         29        CHECK_UNDEF_ARGS                                         
         30        DO_ICALL                                         $14     
         31        VERIFY_RETURN_TYPE                                       $14
         32      > RETURN                                                   $14
   70    33*       VERIFY_RETURN_TYPE                                       
         34*     > RETURN                                                   null

End of function array_transpose_splat

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.62 ms | 1024 KiB | 20 Q