3v4l.org

run code in 500+ 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 = []; 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/Ns8M7
function name:  (null)
number of ops:  28
compiled vars:  !0 = $test
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   72     0  E >   ASSIGN                                                       !0, <array>
   73     1        INIT_FCALL                                                   'var_export'
          2        INIT_FCALL                                                   'array_transpose_basic'
          3        SEND_VAR                                                     !0
          4        DO_FCALL                                          0  $2      
          5        SEND_VAR                                                     $2
          6        SEND_VAL                                                     <true>
          7        DO_ICALL                                             $3      
          8        CONCAT                                               ~4      $3, '%0A---%0A'
          9        ECHO                                                         ~4
   74    10        INIT_FCALL                                                   'var_export'
         11        INIT_FCALL                                                   'array_transpose_types'
         12        SEND_VAR                                                     !0
         13        DO_FCALL                                          0  $5      
         14        SEND_VAR                                                     $5
         15        SEND_VAL                                                     <true>
         16        DO_ICALL                                             $6      
         17        CONCAT                                               ~7      $6, '%0A---%0A'
         18        ECHO                                                         ~7
   75    19        INIT_FCALL                                                   'var_export'
         20        INIT_FCALL                                                   'array_transpose_splat'
         21        SEND_VAR                                                     !0
         22        DO_FCALL                                          0  $8      
         23        SEND_VAR                                                     $8
         24        SEND_VAL                                                     <true>
         25        DO_ICALL                                             $9      
         26        ECHO                                                         $9
         27      > 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/Ns8M7
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 = 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/Ns8M7
function name:  array_transpose_types
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
-----------------------------------------------------------------------------------------
   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, ->80
         16    > > FE_FETCH_R                                           ~19     $18, !2, ->80
         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, ->78
         43    > > FE_FETCH_R                                           ~37     $36, !6, ->78
         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                                                          ->77
   40    57    >   FRAMELESS_ICALL_2                property_exists      ~45     !5, !7
         58        BOOL_NOT                                             ~46     ~45
         59      > JMPZ                                                         ~46, ->68
   41    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
   43    68    >   IS_IDENTICAL                                                 !1, 'array'
         69      > JMPZ                                                         ~51, ->74
   44    70    >   FETCH_OBJ_W                                          $52     !5, !7
         71        ASSIGN_DIM                                                   $52, !3
         72        OP_DATA                                                      !6
   43    73      > JMP                                                          ->77
   46    74    >   FETCH_OBJ_W                                          $54     !5, !7
         75        ASSIGN_OBJ                                                   $54, !3
         76        OP_DATA                                                      !6
   32    77    > > JMP                                                          ->43
         78    >   FE_FREE                                                      $36
   26    79      > JMP                                                          ->16
         80    >   FE_FREE                                                      $18
   51    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
   52    91*       VERIFY_RETURN_TYPE                                           
         92*     > 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/Ns8M7
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.5.0


preferences:
164.13 ms | 1949 KiB | 19 Q