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 unset($list[$oldpos]); $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/IOvBR
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
-------------------------------------------------------------------------------------
   35     0  E >   ASSIGN                                                   !0, <array>
   36     1        ASSIGN                                                   !1, <array>
   38     2        ASSIGN                                                   !2, <array>
   41     3        INIT_FCALL                                               'array_flip'
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                         $14     
          6        ASSIGN                                                   !3, $14
   43     7      > FE_RESET_R                                       $16     !0, ->15
          8    > > FE_FETCH_R                                       ~17     $16, !4, ->15
          9    >   ASSIGN                                                   !5, ~17
   44    10        ISSET_ISEMPTY_DIM_OBJ                         0  ~19     !3, !4
         11        BOOL_NOT                                         ~20     ~19
         12      > JMPZ                                                     ~20, ->14
   45    13    >   UNSET_DIM                                                !0, !5
   43    14    > > JMP                                                      ->8
         15    >   FE_FREE                                                  $16
   50    16        INIT_FCALL                                               'array_values'
         17        SEND_VAR                                                 !0
         18        DO_ICALL                                         $21     
         19        ASSIGN                                                   !0, $21
   51    20        INIT_FCALL                                               'array_values'
         21        SEND_VAR                                                 !1
         22        DO_ICALL                                         $23     
         23        ASSIGN                                                   !1, $23
   53    24        COUNT                                            ~25     !1
         25        SUB                                              ~26     ~25, 1
         26        ASSIGN                                                   !6, ~26
   54    27        INIT_FCALL                                               'array_flip'
         28        SEND_VAR                                                 !0
         29        DO_ICALL                                         $28     
         30        ASSIGN                                                   !7, $28
   57    31        ASSIGN                                                   !8, !6
         32      > JMP                                                      ->75
   59    33    >   FETCH_DIM_R                                      ~31     !1, !8
         34        ASSIGN                                                   !9, ~31
   61    35        ADD                                              ~33     !8, 1
         36        ISSET_ISEMPTY_DIM_OBJ                         0          !1, ~33
         37      > JMPZ                                                     ~34, ->59
   63    38    >   ADD                                              ~35     !8, 1
         39        FETCH_DIM_R                                      ~36     !1, ~35
         40        ASSIGN                                                   !10, ~36
   65    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
   66    46    >   ASSIGN_DIM                                               !2, !9
         47        OP_DATA                                                  !10
   67    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
   68    54        INIT_FCALL                                               'array_flip'
         55        SEND_VAR                                                 !0
         56        DO_ICALL                                         $45     
         57        ASSIGN                                                   !7, $45
         58    > > JMP                                                      ->74
   72    59    >   FETCH_DIM_R                                      ~47     !7, !9
         60        IS_NOT_EQUAL                                             !6, ~47
         61      > JMPZ                                                     ~48, ->74
   73    62    >   ASSIGN_DIM                                               !2, !9
         63        OP_DATA                                                  null
   74    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
   75    70        INIT_FCALL                                               'array_flip'
         71        SEND_VAR                                                 !0
         72        DO_ICALL                                         $52     
         73        ASSIGN                                                   !7, $52
   57    74    >   PRE_DEC                                                  !8
         75    >   IS_SMALLER_OR_EQUAL                                      0, !8
         76      > JMPNZ                                                    ~55, ->33
   80    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 = 26
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 39
Branch analysis from position: 33
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 39
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 42
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
filename:       /in/IOvBR
function name:  orderBeforeArray
number of ops:  51
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, ->26
   15    22    >   UNSET_DIM                                                !2, !3
   16    23        ASSIGN_DIM                                               !2
         24        OP_DATA                                                  !0
         25      > JMP                                                      ->49
   19    26    >   INIT_FCALL                                               'array_search'
         27        SEND_VAR                                                 !1
         28        SEND_VAR                                                 !2
         29        DO_ICALL                                         $15     
         30        ASSIGN                                                   !4, $15
   20    31        TYPE_CHECK                                    4          !4
         32      > JMPZ                                                     ~17, ->39
   21    33    >   NEW                                              $18     'ItemNotFoundException'
         34        NOP                                                      
         35        FAST_CONCAT                                      ~19     !1, '+is+not+a+child+of+this+node'
         36        SEND_VAL_EX                                              ~19
         37        DO_FCALL                                      0          
         38      > THROW                                         0          $18
   23    39    >   IS_SMALLER                                               !3, !4
         40      > JMPZ                                                     ~21, ->42
   25    41    >   PRE_DEC                                                  !4
   27    42    >   UNSET_DIM                                                !2, !3
   28    43        INIT_FCALL                                               'array_splice'
         44        SEND_REF                                                 !2
         45        SEND_VAR                                                 !4
         46        SEND_VAL                                                 0
         47        SEND_VAR                                                 !0
         48        DO_ICALL                                                 
   31    49    > > RETURN                                                   !2
   32    50*     > RETURN                                                   null

End of function orderbeforearray

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
177.99 ms | 1411 KiB | 25 Q