3v4l.org

run code in 300+ PHP versions simultaneously
<?php function insert_after(array &$array, $position, array $values) { // enforce existing position if (!isset($array[$position])) { throw new Exception(strtr('Array position does not exist (:1)', [':1' => $position])); } // offset $offset = 0; // loop through array foreach ($array as $key => $value) { // increase offset ++$offset; // break if key has been found if ($key == $position) { break; } } $array = array_slice($array, 0, $offset, TRUE) + $values + array_slice($array, $offset, NULL, TRUE); return $array; } $original = array( 'a','b','c','d','e','f','d' ); $inserted = array( 'x' ); //$arr = array_splice( $original, 3, 0, $inserted ); $arr = insert_after($original, 3, $inserted); print_r($arr);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BWilE
function name:  (null)
number of ops:  12
compiled vars:  !0 = $original, !1 = $inserted, !2 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   ASSIGN                                                   !0, <array>
   33     1        ASSIGN                                                   !1, <array>
   36     2        INIT_FCALL                                               'insert_after'
          3        SEND_REF                                                 !0
          4        SEND_VAL                                                 3
          5        SEND_VAR                                                 !1
          6        DO_FCALL                                      0  $5      
          7        ASSIGN                                                   !2, $5
   37     8        INIT_FCALL                                               'print_r'
          9        SEND_VAR                                                 !2
         10        DO_ICALL                                                 
         11      > RETURN                                                   1

Function insert_after:
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 = 17, Position 2 = 24
Branch analysis from position: 17
2 jumps found. (Code = 78) Position 1 = 18, Position 2 = 24
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 23
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
Branch analysis from position: 24
Branch analysis from position: 24
filename:       /in/BWilE
function name:  insert_after
number of ops:  42
compiled vars:  !0 = $array, !1 = $position, !2 = $values, !3 = $offset, !4 = $value, !5 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
    6     3        ISSET_ISEMPTY_DIM_OBJ                         0  ~6      !0, !1
          4        BOOL_NOT                                         ~7      ~6
          5      > JMPZ                                                     ~7, ->15
    8     6    >   NEW                                              $8      'Exception'
          7        INIT_FCALL                                               'strtr'
          8        SEND_VAL                                                 'Array+position+does+not+exist+%28%3A1%29'
          9        INIT_ARRAY                                       ~9      !1, '%3A1'
         10        SEND_VAL                                                 ~9
         11        DO_ICALL                                         $10     
         12        SEND_VAR_NO_REF_EX                                       $10
         13        DO_FCALL                                      0          
         14      > THROW                                         0          $8
   12    15    >   ASSIGN                                                   !3, 0
   15    16      > FE_RESET_R                                       $13     !0, ->24
         17    > > FE_FETCH_R                                       ~14     $13, !4, ->24
         18    >   ASSIGN                                                   !5, ~14
   18    19        PRE_INC                                                  !3
   21    20        IS_EQUAL                                                 !5, !1
         21      > JMPZ                                                     ~17, ->23
   23    22    > > JMP                                                      ->24
   15    23    > > JMP                                                      ->17
         24    >   FE_FREE                                                  $13
   27    25        INIT_FCALL                                               'array_slice'
         26        SEND_VAR                                                 !0
         27        SEND_VAL                                                 0
         28        SEND_VAR                                                 !3
         29        SEND_VAL                                                 <true>
         30        DO_ICALL                                         $18     
         31        ADD                                              ~19     $18, !2
         32        INIT_FCALL                                               'array_slice'
         33        SEND_VAR                                                 !0
         34        SEND_VAR                                                 !3
         35        SEND_VAL                                                 null
         36        SEND_VAL                                                 <true>
         37        DO_ICALL                                         $20     
         38        ADD                                              ~21     ~19, $20
         39        ASSIGN                                                   !0, ~21
   29    40      > RETURN                                                   !0
   30    41*     > RETURN                                                   null

End of function insert_after

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.19 ms | 1403 KiB | 20 Q