3v4l.org

run code in 500+ PHP versions simultaneously
<?php function array_transpose(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)[]); } $tests = [ [], (object)[], [['single' => 'row']], ['a' => [1], 'b' => [2, 3]], [['id' => 1], ['id' => 2]], [(object)['x' => 1, 'y' => [[2]]], (object)['a' => null, 'b' => "3"]], (object)['foo' => (object)['bar' => 42], 'goo' => (object)['car' => 24]], [[], []], [[], [], [false]], [[[]], [[]]], ]; foreach ($tests as $test) { echo var_export(array_transpose($test), true) . "\n---\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 22, Position 2 = 33
Branch analysis from position: 22
2 jumps found. (Code = 78) Position 1 = 23, Position 2 = 33
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
filename:       /in/RRKlr
function name:  (null)
number of ops:  35
compiled vars:  !0 = $tests, !1 = $test
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   37     0  E >   INIT_ARRAY                                           ~2      <array>
          1        CAST                                              8  ~3      <array>
          2        ADD_ARRAY_ELEMENT                                    ~2      ~3
          3        ADD_ARRAY_ELEMENT                                    ~2      <array>
          4        ADD_ARRAY_ELEMENT                                    ~2      <array>
          5        ADD_ARRAY_ELEMENT                                    ~2      <array>
          6        CAST                                              8  ~4      <array>
          7        INIT_ARRAY                                           ~5      ~4
          8        CAST                                              8  ~6      <array>
          9        ADD_ARRAY_ELEMENT                                    ~5      ~6
         10        ADD_ARRAY_ELEMENT                                    ~2      ~5
         11        CAST                                              8  ~7      <array>
         12        INIT_ARRAY                                           ~8      ~7, 'foo'
         13        CAST                                              8  ~9      <array>
         14        ADD_ARRAY_ELEMENT                                    ~8      ~9, 'goo'
         15        CAST                                              8  ~10     ~8
         16        ADD_ARRAY_ELEMENT                                    ~2      ~10
         17        ADD_ARRAY_ELEMENT                                    ~2      <array>
         18        ADD_ARRAY_ELEMENT                                    ~2      <array>
         19        ADD_ARRAY_ELEMENT                                    ~2      <array>
   36    20        ASSIGN                                                       !0, ~2
   49    21      > FE_RESET_R                                           $12     !0, ->33
         22    > > FE_FETCH_R                                                   $12, !1, ->33
   50    23    >   INIT_FCALL                                                   'var_export'
         24        INIT_FCALL                                                   'array_transpose'
         25        SEND_VAR                                                     !1
         26        DO_FCALL                                          0  $13     
         27        SEND_VAR                                                     $13
         28        SEND_VAL                                                     <true>
         29        DO_ICALL                                             $14     
         30        CONCAT                                               ~15     $14, '%0A---%0A'
         31        ECHO                                                         ~15
   49    32      > JMP                                                          ->22
         33    >   FE_FREE                                                      $12
   51    34      > RETURN                                                       1

Function array_transpose:
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 = 80
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 80
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 = 78
Branch analysis from position: 43
2 jumps found. (Code = 78) Position 1 = 44, Position 2 = 78
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 = 77
Branch analysis from position: 77
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 = 77
Branch analysis from position: 77
Branch analysis from position: 57
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 68
Branch analysis from position: 60
2 jumps found. (Code = 43) Position 1 = 62, Position 2 = 64
Branch analysis from position: 62
1 jumps found. (Code = 42) Position 1 = 66
Branch analysis from position: 66
2 jumps found. (Code = 43) Position 1 = 70, Position 2 = 74
Branch analysis from position: 70
1 jumps found. (Code = 42) Position 1 = 77
Branch analysis from position: 77
Branch analysis from position: 74
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 64
2 jumps found. (Code = 43) Position 1 = 70, Position 2 = 74
Branch analysis from position: 70
Branch analysis from position: 74
Branch analysis from position: 68
Branch analysis from position: 78
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 78
Branch analysis from position: 37
2 jumps found. (Code = 77) Position 1 = 43, Position 2 = 78
Branch analysis from position: 43
Branch analysis from position: 78
Branch analysis from position: 80
2 jumps found. (Code = 43) Position 1 = 84, Position 2 = 86
Branch analysis from position: 84
1 jumps found. (Code = 42) Position 1 = 88
Branch analysis from position: 88
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 86
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 80
filename:       /in/RRKlr
function name:  array_transpose
number of ops:  93
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
-----------------------------------------------------------------------------------------
    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, ->15
    6     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
    8    15    > > FE_RESET_R                                           $18     !0, ->80
         16    > > FE_FETCH_R                                           ~19     $18, !2, ->80
         17    >   ASSIGN                                                       !3, ~19
    9    18        GET_TYPE                                             ~21     !2
         19        ASSIGN                                                       !4, ~21
   10    20        IN_ARRAY                                             ~23     !4, <array>
         21        BOOL_NOT                                             ~24     ~23
         22      > JMPZ                                                         ~24, ->32
   11    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
   13    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
   14    42      > FE_RESET_R                                           $36     !2, ->78
         43    > > FE_FETCH_R                                           ~37     $36, !6, ->78
         44    >   ASSIGN                                                       !7, ~37
   15    45        IS_IDENTICAL                                                 !4, 'array'
         46      > JMPZ                                                         ~39, ->57
   16    47    >   IS_IDENTICAL                                                 !1, 'array'
         48      > JMPZ                                                         ~40, ->53
   17    49    >   FETCH_DIM_W                                          $41     !5, !7
         50        ASSIGN_DIM                                                   $41, !3
         51        OP_DATA                                                      !6
   16    52      > JMP                                                          ->56
   19    53    >   FETCH_DIM_W                                          $43     !5, !7
         54        ASSIGN_OBJ                                                   $43, !3
         55        OP_DATA                                                      !6
   15    56    > > JMP                                                          ->77
   22    57    >   FRAMELESS_ICALL_2                property_exists      ~45     !5, !7
         58        BOOL_NOT                                             ~46     ~45
         59      > JMPZ                                                         ~46, ->68
   23    60    >   IS_IDENTICAL                                                 !1, 'array'
         61      > JMPZ                                                         ~48, ->64
         62    >   QM_ASSIGN                                            ~49     <array>
         63      > JMP                                                          ->66
         64    >   CAST                                              8  ~50     <array>
         65        QM_ASSIGN                                            ~49     ~50
         66    >   ASSIGN_OBJ                                                   !5, !7
         67        OP_DATA                                                      ~49
   25    68    >   IS_IDENTICAL                                                 !1, 'array'
         69      > JMPZ                                                         ~51, ->74
   26    70    >   FETCH_OBJ_W                                          $52     !5, !7
         71        ASSIGN_DIM                                                   $52, !3
         72        OP_DATA                                                      !6
   25    73      > JMP                                                          ->77
   28    74    >   FETCH_OBJ_W                                          $54     !5, !7
         75        ASSIGN_OBJ                                                   $54, !3
         76        OP_DATA                                                      !6
   14    77    > > JMP                                                          ->43
         78    >   FE_FREE                                                      $36
    8    79      > JMP                                                          ->16
         80    >   FE_FREE                                                      $18
   33    81        COALESCE                                             ~56     !5
         82        IS_IDENTICAL                                                 !1, 'array'
         83      > JMPZ                                                         ~57, ->86
         84    >   QM_ASSIGN                                            ~58     <array>
         85      > JMP                                                          ->88
         86    >   CAST                                              8  ~59     <array>
         87        QM_ASSIGN                                            ~58     ~59
         88    >   QM_ASSIGN                                            ~56     ~58
         89        VERIFY_RETURN_TYPE                                           ~56
         90      > RETURN                                                       ~56
   34    91*       VERIFY_RETURN_TYPE                                           
         92*     > RETURN                                                       null

End of function array_transpose

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
161.15 ms | 1997 KiB | 15 Q