3v4l.org

run code in 300+ 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>
   38     1        CAST                                          8  ~3      <array>
          2        ADD_ARRAY_ELEMENT                                ~2      ~3
   37     3        ADD_ARRAY_ELEMENT                                ~2      <array>
          4        ADD_ARRAY_ELEMENT                                ~2      <array>
          5        ADD_ARRAY_ELEMENT                                ~2      <array>
   42     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
   43    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
   37    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 = 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/RRKlr
function name:  array_transpose
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
-------------------------------------------------------------------------------------
    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, ->83
         16    > > FE_FETCH_R                                       ~19     $18, !2, ->83
         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, ->81
         43    > > FE_FETCH_R                                       ~37     $36, !6, ->81
         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                                                      ->80
   22    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
   23    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
   25    71    >   IS_IDENTICAL                                             !1, 'array'
         72      > JMPZ                                                     ~51, ->77
   26    73    >   FETCH_OBJ_W                                      $52     !5, !7
         74        ASSIGN_DIM                                               $52, !3
         75        OP_DATA                                                  !6
   25    76      > JMP                                                      ->80
   28    77    >   FETCH_OBJ_W                                      $54     !5, !7
         78        ASSIGN_OBJ                                               $54, !3
         79        OP_DATA                                                  !6
   14    80    > > JMP                                                      ->43
         81    >   FE_FREE                                                  $36
    8    82      > JMP                                                      ->16
         83    >   FE_FREE                                                  $18
   33    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
   34    94*       VERIFY_RETURN_TYPE                                       
         95*     > RETURN                                                   null

End of function array_transpose

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.14 ms | 1014 KiB | 16 Q