3v4l.org

run code in 300+ PHP versions simultaneously
<?php function createTree($array){ $tree = []; foreach($array as $v){ $v = str_split($v); $tmp = &$tree; while($c = array_shift($v)) $tmp = &$tmp[strtolower($c)]; unset($tmp); } return $tree; } function findNode($value, $treeTmp){ foreach($value as $k){ if(!array_key_exists(strtolower($k), $treeTmp)) return FALSE; else $treeTmp = $treeTmp[$k]; } return TRUE; } /* ------------ */ $data = ["round", "roundish", "word"]; $searchValue = "round"; //Normal lookU **very slow** $result = in_array($searchValue, $data); var_dump($result); //Faster lookUp $tree = createTree($data); print_r($tree); $result = findNode(str_split($searchValue), $tree); var_dump($result); //PROBLEM -> It also finds values which don't exist in '$data' $searchValue = "ro"; //I DO NOT EXIST IN $data $result = findNode(str_split($searchValue), $tree); var_dump($result); ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tfY9S
function name:  (null)
number of ops:  41
compiled vars:  !0 = $data, !1 = $searchValue, !2 = $result, !3 = $tree
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   ASSIGN                                                   !0, <array>
   40     1        ASSIGN                                                   !1, 'round'
   45     2        INIT_FCALL                                               'in_array'
          3        SEND_VAR                                                 !1
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $6      
          6        ASSIGN                                                   !2, $6
   46     7        INIT_FCALL                                               'var_dump'
          8        SEND_VAR                                                 !2
          9        DO_ICALL                                                 
   49    10        INIT_FCALL                                               'createtree'
         11        SEND_VAR                                                 !0
         12        DO_FCALL                                      0  $9      
         13        ASSIGN                                                   !3, $9
   50    14        INIT_FCALL                                               'print_r'
         15        SEND_VAR                                                 !3
         16        DO_ICALL                                                 
   52    17        INIT_FCALL                                               'findnode'
         18        INIT_FCALL                                               'str_split'
         19        SEND_VAR                                                 !1
         20        DO_ICALL                                         $12     
         21        SEND_VAR                                                 $12
         22        SEND_VAR                                                 !3
         23        DO_FCALL                                      0  $13     
         24        ASSIGN                                                   !2, $13
   53    25        INIT_FCALL                                               'var_dump'
         26        SEND_VAR                                                 !2
         27        DO_ICALL                                                 
   57    28        ASSIGN                                                   !1, 'ro'
   58    29        INIT_FCALL                                               'findnode'
         30        INIT_FCALL                                               'str_split'
         31        SEND_VAR                                                 !1
         32        DO_ICALL                                         $17     
         33        SEND_VAR                                                 $17
         34        SEND_VAR                                                 !3
         35        DO_FCALL                                      0  $18     
         36        ASSIGN                                                   !2, $18
   59    37        INIT_FCALL                                               'var_dump'
         38        SEND_VAR                                                 !2
         39        DO_ICALL                                                 
   61    40      > RETURN                                                   1

Function createtree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 22
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 22
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 10
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 10
Branch analysis from position: 20
Branch analysis from position: 10
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
filename:       /in/tfY9S
function name:  createTree
number of ops:  25
compiled vars:  !0 = $array, !1 = $tree, !2 = $v, !3 = $tmp, !4 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
    6     1        ASSIGN                                                   !1, <array>
    8     2      > FE_RESET_R                                       $6      !0, ->22
          3    > > FE_FETCH_R                                               $6, !2, ->22
    9     4    >   INIT_FCALL                                               'str_split'
          5        SEND_VAR                                                 !2
          6        DO_ICALL                                         $7      
          7        ASSIGN                                                   !2, $7
   10     8        ASSIGN_REF                                               !3, !1
   12     9      > JMP                                                      ->15
   13    10    >   INIT_FCALL                                               'strtolower'
         11        SEND_VAR                                                 !4
         12        DO_ICALL                                         $10     
         13        FETCH_DIM_W                                      $11     !3, $10
         14        ASSIGN_REF                                               !3, $11
   12    15    >   INIT_FCALL                                               'array_shift'
         16        SEND_REF                                                 !2
         17        DO_ICALL                                         $13     
         18        ASSIGN                                           ~14     !4, $13
         19      > JMPNZ                                                    ~14, ->10
   14    20    >   UNSET_CV                                                 !3
    8    21      > JMP                                                      ->3
         22    >   FE_FREE                                                  $6
   17    23      > RETURN                                                   !1
   19    24*     > RETURN                                                   null

End of function createtree

Function findnode:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 16
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 16
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 13
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/tfY9S
function name:  findNode
number of ops:  19
compiled vars:  !0 = $value, !1 = $treeTmp, !2 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   23     2      > FE_RESET_R                                       $3      !0, ->16
          3    > > FE_FETCH_R                                               $3, !2, ->16
   24     4    >   INIT_FCALL                                               'strtolower'
          5        SEND_VAR                                                 !2
          6        DO_ICALL                                         $4      
          7        ARRAY_KEY_EXISTS                                 ~5      $4, !1
          8        BOOL_NOT                                         ~6      ~5
          9      > JMPZ                                                     ~6, ->13
   25    10    >   FE_FREE                                                  $3
         11      > RETURN                                                   <false>
   24    12*       JMP                                                      ->15
   27    13    >   FETCH_DIM_R                                      ~7      !1, !2
         14        ASSIGN                                                   !1, ~7
   23    15      > JMP                                                      ->3
         16    >   FE_FREE                                                  $3
   30    17      > RETURN                                                   <true>
   32    18*     > RETURN                                                   null

End of function findnode

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.25 ms | 1019 KiB | 22 Q