3v4l.org

run code in 300+ PHP versions simultaneously
<?php function subtract(&$arr,$selectedDeletedNumbers){ // pass by reference to edit the same copy of the array foreach($arr as $index => $val){ $low = 0;$high = count($selectedDeletedNumbers) - 1; $equal_found = false; while($low <= $high){ $mid = intval(($low + $high) / 2); if($selectedDeletedNumbers[$mid] > $val){ $high = $mid - 1; }else if($selectedDeletedNumbers[$mid] < $val){ $low = $mid + 1; }else{ $equal_found = true; unset($arr[$index]); // if equal value, delete it as it your need break; } } if(!$equal_found){ $arr[$index] -= $low; // delete the offset till where it is greater among your $selectedDeletedNumbers } } } $selectedDeletedNumbers = [3,5]; sort($selectedDeletedNumbers); // sort to be apply binary search later $arr = [[0, 3, 10, 5, 6, 9, 2, 7, 1, 4, 8, 11], [0, 1], [0, 1], [0, 1]]; foreach($arr as &$val){ // pass by reference to edit the same copy of the array subtract($val,$selectedDeletedNumbers); } print_r($arr);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 6, Position 2 = 12
Branch analysis from position: 6
2 jumps found. (Code = 126) Position 1 = 7, Position 2 = 12
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/RMh5U
function name:  (null)
number of ops:  17
compiled vars:  !0 = $selectedDeletedNumbers, !1 = $arr, !2 = $val
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   ASSIGN                                                   !0, <array>
   27     1        INIT_FCALL                                               'sort'
          2        SEND_REF                                                 !0
          3        DO_ICALL                                                 
   28     4        ASSIGN                                                   !1, <array>
   30     5      > FE_RESET_RW                                      $6      !1, ->12
          6    > > FE_FETCH_RW                                              $6, !2, ->12
   31     7    >   INIT_FCALL                                               'subtract'
          8        SEND_REF                                                 !2
          9        SEND_VAR                                                 !0
         10        DO_FCALL                                      0          
   30    11      > JMP                                                      ->6
         12    >   FE_FREE                                                  $6
   34    13        INIT_FCALL                                               'print_r'
         14        SEND_VAR                                                 !1
         15        DO_ICALL                                                 
         16      > RETURN                                                   1

Function subtract:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 37
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 37
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 11
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 36
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 36
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 21
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 27
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
filename:       /in/RMh5U
function name:  subtract
number of ops:  39
compiled vars:  !0 = $arr, !1 = $selectedDeletedNumbers, !2 = $val, !3 = $index, !4 = $low, !5 = $high, !6 = $equal_found, !7 = $mid
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    4     2      > FE_RESET_R                                       $8      !0, ->37
          3    > > FE_FETCH_R                                       ~9      $8, !2, ->37
          4    >   ASSIGN                                                   !3, ~9
    5     5        ASSIGN                                                   !4, 0
          6        COUNT                                            ~12     !1
          7        SUB                                              ~13     ~12, 1
          8        ASSIGN                                                   !5, ~13
    6     9        ASSIGN                                                   !6, <false>
    7    10      > JMP                                                      ->30
    8    11    >   ADD                                              ~16     !4, !5
         12        DIV                                              ~17     ~16, 2
         13        CAST                                          4  ~18     ~17
         14        ASSIGN                                                   !7, ~18
    9    15        FETCH_DIM_R                                      ~20     !1, !7
         16        IS_SMALLER                                               !2, ~20
         17      > JMPZ                                                     ~21, ->21
   10    18    >   SUB                                              ~22     !7, 1
         19        ASSIGN                                                   !5, ~22
    9    20      > JMP                                                      ->30
   11    21    >   FETCH_DIM_R                                      ~24     !1, !7
         22        IS_SMALLER                                               ~24, !2
         23      > JMPZ                                                     ~25, ->27
   12    24    >   ADD                                              ~26     !7, 1
         25        ASSIGN                                                   !4, ~26
   11    26      > JMP                                                      ->30
   14    27    >   ASSIGN                                                   !6, <true>
   15    28        UNSET_DIM                                                !0, !3
   16    29      > JMP                                                      ->32
    7    30    >   IS_SMALLER_OR_EQUAL                                      !4, !5
         31      > JMPNZ                                                    ~29, ->11
   20    32    >   BOOL_NOT                                         ~30     !6
         33      > JMPZ                                                     ~30, ->36
   21    34    >   ASSIGN_DIM_OP                -=               2          !0, !3
         35        OP_DATA                                                  !4
    4    36    > > JMP                                                      ->3
         37    >   FE_FREE                                                  $8
   24    38      > RETURN                                                   null

End of function subtract

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
146.35 ms | 1441 KiB | 16 Q