3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Reposition an array element by on its key. * * @param array $array The array being reordered. * @param string|int $key They key of the element you want to reposition. * @param int $order The position in the array you want to move the element to. (0 is first) * * @throws \Exception */ function repositionArrayElement(array &$array, $key, int $order): void { if(($a = array_search($key, array_keys($array))) === false){ throw new \Exception("The {$key} cannot be found in the given array."); } $p1 = array_splice($array, $a, 1); $p2 = array_splice($array, 0, $order); $array = array_merge($p2, $p1, $array); } $fruits = [ 'bananas'=>'12', 'apples'=>'23', 'tomatoes'=>'21', 'nuts'=>'22', 'foo'=>'a', 'bar'=>'b' ]; repositionArrayElement($fruits, "foo", 1); var_export($fruits); $colours = ["green", "blue", "red"]; repositionArrayElement($colours, 2, 0); var_export($colours);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1QgKX
function name:  (null)
number of ops:  19
compiled vars:  !0 = $fruits, !1 = $colours
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   ASSIGN                                                   !0, <array>
   31     1        INIT_FCALL                                               'repositionarrayelement'
          2        SEND_REF                                                 !0
          3        SEND_VAL                                                 'foo'
          4        SEND_VAL                                                 1
          5        DO_FCALL                                      0          
   33     6        INIT_FCALL                                               'var_export'
          7        SEND_VAR                                                 !0
          8        DO_ICALL                                                 
   35     9        ASSIGN                                                   !1, <array>
   37    10        INIT_FCALL                                               'repositionarrayelement'
         11        SEND_REF                                                 !1
         12        SEND_VAL                                                 2
         13        SEND_VAL                                                 0
         14        DO_FCALL                                      0          
   39    15        INIT_FCALL                                               'var_export'
         16        SEND_VAR                                                 !1
         17        DO_ICALL                                                 
         18      > RETURN                                                   1

Function repositionarrayelement:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 20
Branch analysis from position: 13
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1QgKX
function name:  repositionArrayElement
number of ops:  39
compiled vars:  !0 = $array, !1 = $key, !2 = $order, !3 = $a, !4 = $p1, !5 = $p2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   14     3        INIT_FCALL                                               'array_search'
          4        SEND_VAR                                                 !1
          5        INIT_FCALL                                               'array_keys'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $6      
          8        SEND_VAR                                                 $6
          9        DO_ICALL                                         $7      
         10        ASSIGN                                           ~8      !3, $7
         11        TYPE_CHECK                                    4          ~8
         12      > JMPZ                                                     ~9, ->20
   15    13    >   NEW                                              $10     'Exception'
         14        ROPE_INIT                                     3  ~12     'The+'
         15        ROPE_ADD                                      1  ~12     ~12, !1
         16        ROPE_END                                      2  ~11     ~12, '+cannot+be+found+in+the+given+array.'
         17        SEND_VAL_EX                                              ~11
         18        DO_FCALL                                      0          
         19      > THROW                                         0          $10
   17    20    >   INIT_FCALL                                               'array_splice'
         21        SEND_REF                                                 !0
         22        SEND_VAR                                                 !3
         23        SEND_VAL                                                 1
         24        DO_ICALL                                         $15     
         25        ASSIGN                                                   !4, $15
   18    26        INIT_FCALL                                               'array_splice'
         27        SEND_REF                                                 !0
         28        SEND_VAL                                                 0
         29        SEND_VAR                                                 !2
         30        DO_ICALL                                         $17     
         31        ASSIGN                                                   !5, $17
   19    32        INIT_FCALL                                               'array_merge'
         33        SEND_VAR                                                 !5
         34        SEND_VAR                                                 !4
         35        SEND_VAR                                                 !0
         36        DO_ICALL                                         $19     
         37        ASSIGN                                                   !0, $19
   20    38      > RETURN                                                   null

End of function repositionarrayelement

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.01 ms | 1465 KiB | 20 Q