3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * An array in DESCENDING order (OP) */ $array = [ 61029, 64698, 67355, 70000, // has bubble 43651, 48346, 52029, // has bubble 48029, 48698, 49355, 50000, ]; /** * An array in ASCENDING order */ $asc_array = [ 10, 20, 30, 40, 50, 45, //Has bubble 55 ]; /** * Given an array, identify a "bubble", aka. an increasing value in an otherwise decreasing value array. * Returns the $key where the bubble resides. */ function identifyBubble($array){ foreach($array as $id => $item){ if(!$id){ continue; } if(!$array[$id+1]){ continue; } if(($array[$id-1] < $array[$id]) && ($array[$id] > $array[$id+1])){ return $id; } } return false; } /** * If an array is in ASCENDING order, switch it around, * otherwise return the array as is. */ function makeArrayDescending($array){ if(reset($array) < end($array)){ return array_values(array_reverse($array)); } return $array; } var_dump(identifyBubble($array)); var_dump(makeArrayDescending($asc_array)); var_dump(identifyBubble(makeArrayDescending($asc_array)));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9UNY6
function name:  (null)
number of ops:  24
compiled vars:  !0 = $array, !1 = $asc_array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   ASSIGN                                                   !0, <array>
   23     1        ASSIGN                                                   !1, <array>
   63     2        INIT_FCALL                                               'var_dump'
          3        INIT_FCALL                                               'identifybubble'
          4        SEND_VAR                                                 !0
          5        DO_FCALL                                      0  $4      
          6        SEND_VAR                                                 $4
          7        DO_ICALL                                                 
   65     8        INIT_FCALL                                               'var_dump'
          9        INIT_FCALL                                               'makearraydescending'
         10        SEND_VAR                                                 !1
         11        DO_FCALL                                      0  $6      
         12        SEND_VAR                                                 $6
         13        DO_ICALL                                                 
   67    14        INIT_FCALL                                               'var_dump'
         15        INIT_FCALL                                               'identifybubble'
         16        INIT_FCALL                                               'makearraydescending'
         17        SEND_VAR                                                 !1
         18        DO_FCALL                                      0  $8      
         19        SEND_VAR                                                 $8
         20        DO_FCALL                                      0  $9      
         21        SEND_VAR                                                 $9
         22        DO_ICALL                                                 
         23      > RETURN                                                   1

Function identifybubble:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 26
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 26
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 12
2 jumps found. (Code = 46) Position 1 = 17, Position 2 = 22
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 25
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 22
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
filename:       /in/9UNY6
function name:  identifyBubble
number of ops:  29
compiled vars:  !0 = $array, !1 = $item, !2 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
   38     1      > FE_RESET_R                                       $3      !0, ->26
          2    > > FE_FETCH_R                                       ~4      $3, !1, ->26
          3    >   ASSIGN                                                   !2, ~4
   39     4        BOOL_NOT                                         ~6      !2
          5      > JMPZ                                                     ~6, ->7
   40     6    > > JMP                                                      ->2
   42     7    >   ADD                                              ~7      !2, 1
          8        FETCH_DIM_R                                      ~8      !0, ~7
          9        BOOL_NOT                                         ~9      ~8
         10      > JMPZ                                                     ~9, ->12
   43    11    > > JMP                                                      ->2
   45    12    >   SUB                                              ~10     !2, 1
         13        FETCH_DIM_R                                      ~11     !0, ~10
         14        FETCH_DIM_R                                      ~12     !0, !2
         15        IS_SMALLER                                       ~13     ~11, ~12
         16      > JMPZ_EX                                          ~13     ~13, ->22
         17    >   FETCH_DIM_R                                      ~14     !0, !2
         18        ADD                                              ~15     !2, 1
         19        FETCH_DIM_R                                      ~16     !0, ~15
         20        IS_SMALLER                                       ~17     ~16, ~14
         21        BOOL                                             ~13     ~17
         22    > > JMPZ                                                     ~13, ->25
   46    23    >   FE_FREE                                                  $3
         24      > RETURN                                                   !2
   38    25    > > JMP                                                      ->2
         26    >   FE_FREE                                                  $3
   49    27      > RETURN                                                   <false>
   50    28*     > RETURN                                                   null

End of function identifybubble

Function makearraydescending:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 16
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9UNY6
function name:  makeArrayDescending
number of ops:  18
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   RECV                                             !0      
   57     1        INIT_FCALL                                               'reset'
          2        SEND_REF                                                 !0
          3        DO_ICALL                                         $1      
          4        INIT_FCALL                                               'end'
          5        SEND_REF                                                 !0
          6        DO_ICALL                                         $2      
          7        IS_SMALLER                                               $1, $2
          8      > JMPZ                                                     ~3, ->16
   58     9    >   INIT_FCALL                                               'array_values'
         10        INIT_FCALL                                               'array_reverse'
         11        SEND_VAR                                                 !0
         12        DO_ICALL                                         $4      
         13        SEND_VAR                                                 $4
         14        DO_ICALL                                         $5      
         15      > RETURN                                                   $5
   60    16    > > RETURN                                                   !0
   61    17*     > RETURN                                                   null

End of function makearraydescending

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
181.74 ms | 1417 KiB | 27 Q