3v4l.org

run code in 300+ PHP versions simultaneously
<?php // This code is public domain. The original author is Marc Ermshaus. error_reporting(-1); /** * Circularly shifts an array * * Shifts to right for $steps > 0. Shifts to left for $steps < 0. Keys are * preserved. * * @param array $array Array to shift * @param int $steps Steps to shift array by * @return array Resulting array */ function array_circular(array $array, $steps = 1) { if (!is_int($steps)) { throw new InvalidArgumentException( 'steps has to be an (int)'); } if ($steps === 0) { return $array; } $l = count($array); if ($l === 0) { return $array; } $steps = $steps % $l; $steps *= -1; return array_merge(array_slice($array, $steps), array_slice($array, 0, $steps)); } header('content-type: text/plain'); $a = array("1" => "Primeiro", "2"=>"Segundo", "3"=>"Terceiro", "4"=> "Quarto"); $l = count($a); for ($i = $l * -2; $i <= $l * 2; $i++) { printf("% 3s : %s\n", $i, implode(', ', array_shift_circular($a, $i))); } ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 12
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 12
Branch analysis from position: 29
Branch analysis from position: 12
filename:       /in/de2Nb
function name:  (null)
number of ops:  30
compiled vars:  !0 = $a, !1 = $l, !2 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                               'error_reporting'
          1        SEND_VAL                                                 -1
          2        DO_ICALL                                                 
   41     3        INIT_FCALL                                               'header'
          4        SEND_VAL                                                 'content-type%3A+text%2Fplain'
          5        DO_ICALL                                                 
   43     6        ASSIGN                                                   !0, <array>
   45     7        COUNT                                            ~6      !0
          8        ASSIGN                                                   !1, ~6
   47     9        MUL                                              ~8      !1, -2
         10        ASSIGN                                                   !2, ~8
         11      > JMP                                                      ->26
   48    12    >   INIT_FCALL                                               'printf'
         13        SEND_VAL                                                 '%25+3s++%3A++%25s%0A'
         14        SEND_VAR                                                 !2
         15        INIT_FCALL                                               'implode'
         16        SEND_VAL                                                 '%2C+'
         17        INIT_FCALL_BY_NAME                                       'array_shift_circular'
         18        SEND_VAR_EX                                              !0
         19        SEND_VAR_EX                                              !2
         20        DO_FCALL                                      0  $10     
         21        SEND_VAR                                                 $10
         22        DO_ICALL                                         $11     
         23        SEND_VAR                                                 $11
         24        DO_ICALL                                                 
   47    25        PRE_INC                                                  !2
         26    >   MUL                                              ~14     !1, 2
         27        IS_SMALLER_OR_EQUAL                                      !2, ~14
         28      > JMPNZ                                                    ~15, ->12
   50    29    > > RETURN                                                   1

Function array_circular:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 17
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/de2Nb
function name:  array_circular
number of ops:  35
compiled vars:  !0 = $array, !1 = $steps, !2 = $l
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      1
   17     2        TYPE_CHECK                                   16  ~3      !1
          3        BOOL_NOT                                         ~4      ~3
          4      > JMPZ                                                     ~4, ->9
   18     5    >   NEW                                              $5      'InvalidArgumentException'
   19     6        SEND_VAL_EX                                              'steps+has+to+be+an+%28int%29'
          7        DO_FCALL                                      0          
          8      > THROW                                         0          $5
   22     9    >   IS_IDENTICAL                                             !1, 0
         10      > JMPZ                                                     ~7, ->12
   23    11    > > RETURN                                                   !0
   26    12    >   COUNT                                            ~8      !0
         13        ASSIGN                                                   !2, ~8
   28    14        IS_IDENTICAL                                             !2, 0
         15      > JMPZ                                                     ~10, ->17
   29    16    > > RETURN                                                   !0
   32    17    >   MOD                                              ~11     !1, !2
         18        ASSIGN                                                   !1, ~11
   33    19        ASSIGN_OP                                     3          !1, -1
   35    20        INIT_FCALL                                               'array_merge'
         21        INIT_FCALL                                               'array_slice'
         22        SEND_VAR                                                 !0
         23        SEND_VAR                                                 !1
         24        DO_ICALL                                         $14     
         25        SEND_VAR                                                 $14
   36    26        INIT_FCALL                                               'array_slice'
         27        SEND_VAR                                                 !0
         28        SEND_VAL                                                 0
         29        SEND_VAR                                                 !1
         30        DO_ICALL                                         $15     
         31        SEND_VAR                                                 $15
         32        DO_ICALL                                         $16     
         33      > RETURN                                                   $16
   37    34*     > RETURN                                                   null

End of function array_circular

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
130.75 ms | 1405 KiB | 25 Q