3v4l.org

run code in 300+ PHP versions simultaneously
<?php function orderBeforeArray($name, $destination, $list) { // reindex the array so there are no gaps $list = array_values($list); $oldpos = array_search($name, $list); if (false === $oldpos) { throw new ItemNotFoundException("$name is not a child of this node"); } if ($destination == null) { // null means move to end $list = array_filter($list, function ($index) use ($oldpos) { return $index !== $oldpos; }, ARRAY_FILTER_USE_KEY); $list[] = $name; } else { // insert before element $destination $newpos = array_search($destination, $list); if ($newpos === false) { throw new ItemNotFoundException("$destination is not a child of this node"); } if ($oldpos < $newpos) { // we first unset, the position will change by one $newpos--; } unset($list[$oldpos]); array_splice($list, $newpos, 0, $name); } return $list; } $old = ['one', 'two', 'three', 'four']; $new = ['one', 'three', 'two']; $reorders = []; //check for deleted items $newIndex = array_flip($new); foreach ($old as $key => $value) { if (!isset($newIndex[$value])) { unset($old[$key]); } } // reindex the arrays to avoid holes in the indexes $old = array_values($old); $new = array_values($new); $len = count($new) - 1; $oldIndex = array_flip($old); //go backwards on the new node order and arrange them this way for ($i = $len; $i >= 0; $i--) { //get the name of the child node $current = $new[$i]; //check if it's not the last node if (isset($new[$i + 1])) { // get the name of the next node $next = $new[$i + 1]; //if in the old order $c and next are not neighbors already, do the reorder command if ($oldIndex[$current] + 1 != $oldIndex[$next]) { $reorders[$current] = $next; $old = orderBeforeArray($current, $next, $old); $oldIndex = array_flip($old); } } else { //check if it's not already at the end of the nodes if ($oldIndex[$current] != $len) { $reorders[$current] = null; $old = orderBeforeArray($current, null, $old); $oldIndex = array_flip($old); } } } var_dump($reorders);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 15
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 15
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 14
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 14
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
2 jumps found. (Code = 44) Position 1 = 77, Position 2 = 33
Branch analysis from position: 77
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 59
Branch analysis from position: 38
2 jumps found. (Code = 43) Position 1 = 46, Position 2 = 58
Branch analysis from position: 46
1 jumps found. (Code = 42) Position 1 = 74
Branch analysis from position: 74
2 jumps found. (Code = 44) Position 1 = 77, Position 2 = 33
Branch analysis from position: 77
Branch analysis from position: 33
Branch analysis from position: 58
Branch analysis from position: 59
2 jumps found. (Code = 43) Position 1 = 62, Position 2 = 74
Branch analysis from position: 62
2 jumps found. (Code = 44) Position 1 = 77, Position 2 = 33
Branch analysis from position: 77
Branch analysis from position: 33
Branch analysis from position: 74
Branch analysis from position: 15
filename:       /in/gG7BG
function name:  (null)
number of ops:  81
compiled vars:  !0 = $old, !1 = $new, !2 = $reorders, !3 = $newIndex, !4 = $value, !5 = $key, !6 = $len, !7 = $oldIndex, !8 = $i, !9 = $current, !10 = $next
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   ASSIGN                                                   !0, <array>
   38     1        ASSIGN                                                   !1, <array>
   40     2        ASSIGN                                                   !2, <array>
   43     3        INIT_FCALL                                               'array_flip'
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                         $14     
          6        ASSIGN                                                   !3, $14
   45     7      > FE_RESET_R                                       $16     !0, ->15
          8    > > FE_FETCH_R                                       ~17     $16, !4, ->15
          9    >   ASSIGN                                                   !5, ~17
   46    10        ISSET_ISEMPTY_DIM_OBJ                         0  ~19     !3, !4
         11        BOOL_NOT                                         ~20     ~19
         12      > JMPZ                                                     ~20, ->14
   47    13    >   UNSET_DIM                                                !0, !5
   45    14    > > JMP                                                      ->8
         15    >   FE_FREE                                                  $16
   52    16        INIT_FCALL                                               'array_values'
         17        SEND_VAR                                                 !0
         18        DO_ICALL                                         $21     
         19        ASSIGN                                                   !0, $21
   53    20        INIT_FCALL                                               'array_values'
         21        SEND_VAR                                                 !1
         22        DO_ICALL                                         $23     
         23        ASSIGN                                                   !1, $23
   55    24        COUNT                                            ~25     !1
         25        SUB                                              ~26     ~25, 1
         26        ASSIGN                                                   !6, ~26
   56    27        INIT_FCALL                                               'array_flip'
         28        SEND_VAR                                                 !0
         29        DO_ICALL                                         $28     
         30        ASSIGN                                                   !7, $28
   59    31        ASSIGN                                                   !8, !6
         32      > JMP                                                      ->75
   61    33    >   FETCH_DIM_R                                      ~31     !1, !8
         34        ASSIGN                                                   !9, ~31
   63    35        ADD                                              ~33     !8, 1
         36        ISSET_ISEMPTY_DIM_OBJ                         0          !1, ~33
         37      > JMPZ                                                     ~34, ->59
   65    38    >   ADD                                              ~35     !8, 1
         39        FETCH_DIM_R                                      ~36     !1, ~35
         40        ASSIGN                                                   !10, ~36
   67    41        FETCH_DIM_R                                      ~38     !7, !9
         42        ADD                                              ~39     ~38, 1
         43        FETCH_DIM_R                                      ~40     !7, !10
         44        IS_NOT_EQUAL                                             ~39, ~40
         45      > JMPZ                                                     ~41, ->58
   68    46    >   ASSIGN_DIM                                               !2, !9
         47        OP_DATA                                                  !10
   69    48        INIT_FCALL                                               'orderbeforearray'
         49        SEND_VAR                                                 !9
         50        SEND_VAR                                                 !10
         51        SEND_VAR                                                 !0
         52        DO_FCALL                                      0  $43     
         53        ASSIGN                                                   !0, $43
   70    54        INIT_FCALL                                               'array_flip'
         55        SEND_VAR                                                 !0
         56        DO_ICALL                                         $45     
         57        ASSIGN                                                   !7, $45
   63    58    > > JMP                                                      ->74
   74    59    >   FETCH_DIM_R                                      ~47     !7, !9
         60        IS_NOT_EQUAL                                             !6, ~47
         61      > JMPZ                                                     ~48, ->74
   75    62    >   ASSIGN_DIM                                               !2, !9
         63        OP_DATA                                                  null
   76    64        INIT_FCALL                                               'orderbeforearray'
         65        SEND_VAR                                                 !9
         66        SEND_VAL                                                 null
         67        SEND_VAR                                                 !0
         68        DO_FCALL                                      0  $50     
         69        ASSIGN                                                   !0, $50
   77    70        INIT_FCALL                                               'array_flip'
         71        SEND_VAR                                                 !0
         72        DO_ICALL                                         $52     
         73        ASSIGN                                                   !7, $52
   59    74    >   PRE_DEC                                                  !8
         75    >   IS_SMALLER_OR_EQUAL                                      0, !8
         76      > JMPNZ                                                    ~55, ->33
   82    77    >   INIT_FCALL                                               'var_dump'
         78        SEND_VAR                                                 !2
         79        DO_ICALL                                                 
         80      > RETURN                                                   1

Function orderbeforearray:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 20
Branch analysis from position: 14
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 33
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 56
Branch analysis from position: 56
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 40, Position 2 = 46
Branch analysis from position: 40
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 46
2 jumps found. (Code = 43) Position 1 = 48, Position 2 = 49
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 49
filename:       /in/gG7BG
function name:  orderBeforeArray
number of ops:  58
compiled vars:  !0 = $name, !1 = $destination, !2 = $list, !3 = $oldpos, !4 = $newpos
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
    6     3        INIT_FCALL                                               'array_values'
          4        SEND_VAR                                                 !2
          5        DO_ICALL                                         $5      
          6        ASSIGN                                                   !2, $5
    7     7        INIT_FCALL                                               'array_search'
          8        SEND_VAR                                                 !0
          9        SEND_VAR                                                 !2
         10        DO_ICALL                                         $7      
         11        ASSIGN                                                   !3, $7
    9    12        TYPE_CHECK                                    4          !3
         13      > JMPZ                                                     ~9, ->20
   10    14    >   NEW                                              $10     'ItemNotFoundException'
         15        NOP                                                      
         16        FAST_CONCAT                                      ~11     !0, '+is+not+a+child+of+this+node'
         17        SEND_VAL_EX                                              ~11
         18        DO_FCALL                                      0          
         19      > THROW                                         0          $10
   13    20    >   IS_EQUAL                                                 !1, null
         21      > JMPZ                                                     ~13, ->33
   15    22    >   INIT_FCALL                                               'array_filter'
         23        SEND_VAR                                                 !2
         24        DECLARE_LAMBDA_FUNCTION                          ~14     [0]
         25        BIND_LEXICAL                                             ~14, !3
   17    26        SEND_VAL                                                 ~14
         27        SEND_VAL                                                 2
   15    28        DO_ICALL                                         $15     
         29        ASSIGN                                                   !2, $15
   18    30        ASSIGN_DIM                                               !2
         31        OP_DATA                                                  !0
   13    32      > JMP                                                      ->56
   21    33    >   INIT_FCALL                                               'array_search'
         34        SEND_VAR                                                 !1
         35        SEND_VAR                                                 !2
         36        DO_ICALL                                         $18     
         37        ASSIGN                                                   !4, $18
   22    38        TYPE_CHECK                                    4          !4
         39      > JMPZ                                                     ~20, ->46
   23    40    >   NEW                                              $21     'ItemNotFoundException'
         41        NOP                                                      
         42        FAST_CONCAT                                      ~22     !1, '+is+not+a+child+of+this+node'
         43        SEND_VAL_EX                                              ~22
         44        DO_FCALL                                      0          
         45      > THROW                                         0          $21
   25    46    >   IS_SMALLER                                               !3, !4
         47      > JMPZ                                                     ~24, ->49
   27    48    >   PRE_DEC                                                  !4
   29    49    >   UNSET_DIM                                                !2, !3
   30    50        INIT_FCALL                                               'array_splice'
         51        SEND_REF                                                 !2
         52        SEND_VAR                                                 !4
         53        SEND_VAL                                                 0
         54        SEND_VAR                                                 !0
         55        DO_ICALL                                                 
   33    56    > > RETURN                                                   !2
   34    57*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/gG7BG
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $index, !1 = $oldpos
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   16     2        IS_NOT_IDENTICAL                                 ~2      !0, !1
          3      > RETURN                                                   ~2
   17     4*     > RETURN                                                   null

End of Dynamic Function 0

End of function orderbeforearray

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.76 ms | 1457 KiB | 21 Q