3v4l.org

run code in 300+ PHP versions simultaneously
<?php function spiralmatrix($arr) { return $arr ? array_merge( array_shift($arr), spiralmatrix(array_reverse(transpose($arr))) ) : $arr; } function transpose($arr) { return $arr ? @!$arr[1] ? array_chunk($arr[0], 1) : array_map(null, ...$arr) : $arr; } print_r(spiralmatrix([])); print_r(spiralmatrix([[1]])); print_r(spiralmatrix([[1,2],[4,3]])); print_r(spiralmatrix([[1,2,3],[8,9,4],[7,6,5]])); print_r(spiralmatrix([[1,2,3,4],[12,13,14,5],[11,16,15,6],[10,9,8,7]])); print_r(spiralmatrix([[1,2,3,4],[10,11,12,5],[9,8,7,6]]));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RSQ5j
function name:  (null)
number of ops:  37
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   INIT_FCALL                                               'print_r'
          1        INIT_FCALL                                               'spiralmatrix'
          2        SEND_VAL                                                 <array>
          3        DO_FCALL                                      0  $0      
          4        SEND_VAR                                                 $0
          5        DO_ICALL                                                 
   22     6        INIT_FCALL                                               'print_r'
          7        INIT_FCALL                                               'spiralmatrix'
          8        SEND_VAL                                                 <array>
          9        DO_FCALL                                      0  $2      
         10        SEND_VAR                                                 $2
         11        DO_ICALL                                                 
   23    12        INIT_FCALL                                               'print_r'
         13        INIT_FCALL                                               'spiralmatrix'
         14        SEND_VAL                                                 <array>
         15        DO_FCALL                                      0  $4      
         16        SEND_VAR                                                 $4
         17        DO_ICALL                                                 
   24    18        INIT_FCALL                                               'print_r'
         19        INIT_FCALL                                               'spiralmatrix'
         20        SEND_VAL                                                 <array>
         21        DO_FCALL                                      0  $6      
         22        SEND_VAR                                                 $6
         23        DO_ICALL                                                 
   25    24        INIT_FCALL                                               'print_r'
         25        INIT_FCALL                                               'spiralmatrix'
         26        SEND_VAL                                                 <array>
         27        DO_FCALL                                      0  $8      
         28        SEND_VAR                                                 $8
         29        DO_ICALL                                                 
   26    30        INIT_FCALL                                               'print_r'
         31        INIT_FCALL                                               'spiralmatrix'
         32        SEND_VAL                                                 <array>
         33        DO_FCALL                                      0  $10     
         34        SEND_VAR                                                 $10
         35        DO_ICALL                                                 
         36      > RETURN                                                   1

Function spiralmatrix:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 20
Branch analysis from position: 2
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RSQ5j
function name:  spiralmatrix
number of ops:  23
compiled vars:  !0 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
    4     1      > JMPZ                                                     !0, ->20
    5     2    >   INIT_FCALL                                               'array_merge'
    6     3        INIT_FCALL                                               'array_shift'
          4        SEND_REF                                                 !0
          5        DO_ICALL                                         $1      
          6        SEND_VAR                                                 $1
    7     7        INIT_FCALL_BY_NAME                                       'spiralmatrix'
          8        INIT_FCALL                                               'array_reverse'
          9        INIT_FCALL_BY_NAME                                       'transpose'
         10        SEND_VAR_EX                                              !0
         11        DO_FCALL                                      0  $2      
         12        SEND_VAR                                                 $2
         13        DO_ICALL                                         $3      
         14        SEND_VAR_NO_REF_EX                                       $3
         15        DO_FCALL                                      0  $4      
         16        SEND_VAR                                                 $4
    5    17        DO_ICALL                                         $5      
    7    18        QM_ASSIGN                                        ~6      $5
         19      > JMP                                                      ->21
    9    20    >   QM_ASSIGN                                        ~6      !0
         21    > > RETURN                                                   ~6
   10    22*     > RETURN                                                   null

End of function spiralmatrix

Function transpose:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 22
Branch analysis from position: 2
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 14
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/RSQ5j
function name:  transpose
number of ops:  25
compiled vars:  !0 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
   14     1      > JMPZ                                                     !0, ->22
   15     2    >   BEGIN_SILENCE                                    ~1      
          3        FETCH_DIM_R                                      ~2      !0, 1
          4        BOOL_NOT                                         ~3      ~2
          5        END_SILENCE                                              ~1
          6      > JMPZ                                                     ~3, ->14
   16     7    >   INIT_FCALL                                               'array_chunk'
          8        FETCH_DIM_R                                      ~4      !0, 0
          9        SEND_VAL                                                 ~4
         10        SEND_VAL                                                 1
         11        DO_ICALL                                         $5      
         12        QM_ASSIGN                                        ~6      $5
         13      > JMP                                                      ->20
   17    14    >   INIT_FCALL                                               'array_map'
         15        SEND_VAL                                                 null
         16        SEND_UNPACK                                              !0
         17        CHECK_UNDEF_ARGS                                         
         18        DO_ICALL                                         $7      
         19        QM_ASSIGN                                        ~6      $7
         20    >   QM_ASSIGN                                        ~8      ~6
         21      > JMP                                                      ->23
   18    22    >   QM_ASSIGN                                        ~8      !0
         23    > > RETURN                                                   ~8
   19    24*     > RETURN                                                   null

End of function transpose

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
299.18 ms | 1027 KiB | 25 Q