3v4l.org

run code in 300+ PHP versions simultaneously
<?php echo '<pre>'; $id = 1035; // Searching ID $myObj = array(); $a = [ 'id'=> 291, 'children' => [ [ 'id' => 1034, 'children' => [ [ 'id' => 111, 'name' => 'ABC', 'figure' => '6 digits', 'children'=> [] ], [ 'id' => 1035, 'lft' => 'LEFT', 'children' => [ [ 'id' => 1036, 'children' => [ [ 'id' => 222, 'someKey' => 'some value', 'children'=> [] ] ] ], [ 'id' => 333, 'someKey' => 'some value', 'children'=> [] ] ], ] ], ], [ 'id' => 1024, 'title' => 'ABC', 'children' => [ ], ] ] ]; function findObject($id, $obj) { global $myObj; // This is an object. if (isset($obj["id"])) { echo "Checking {$obj["id"]}<br />"; // Check the id to what we need. if ($obj["id"] == $id) { // Yay! We found it. Return the object. echo "<strong>Yay we found {$obj["id"]}</strong><br />"; $myObj = $obj; echo "<strong>Need to find a way to break out!</strong><br />"; } else { echo "Checking children of {$obj["id"]}<br />"; // See if it has any children if (isset($obj["children"]) && count($obj["children"]) > 0) { echo "There are children for {$obj["id"]}<br />"; foreach ($obj["children"] as $child) { findObject($id, $child); } } } } } findObject($id, $a); if (isset($myObj) && !empty($myObj)) { echo "<br /><strong>Found it!</strong><br />"; print_r($myObj); } else echo "Sorry, can't find the ID!"; echo '</pre>';
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 10, Position 2 = 13
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 19
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/rWkPq
function name:  (null)
number of ops:  22
compiled vars:  !0 = $id, !1 = $myObj, !2 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ECHO                                                     '%3Cpre%3E'
    3     1        ASSIGN                                                   !0, 1035
    4     2        ASSIGN                                                   !1, <array>
    6     3        ASSIGN                                                   !2, <array>
   76     4        INIT_FCALL                                               'findobject'
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !2
          7        DO_FCALL                                      0          
   77     8        ISSET_ISEMPTY_CV                                 ~7      !1
          9      > JMPZ_EX                                          ~7      ~7, ->13
         10    >   ISSET_ISEMPTY_CV                                 ~8      !1
         11        BOOL_NOT                                         ~9      ~8
         12        BOOL                                             ~7      ~9
         13    > > JMPZ                                                     ~7, ->19
   78    14    >   ECHO                                                     '%3Cbr+%2F%3E%3Cstrong%3EFound+it%21%3C%2Fstrong%3E%3Cbr+%2F%3E'
   79    15        INIT_FCALL                                               'print_r'
         16        SEND_VAR                                                 !1
         17        DO_ICALL                                                 
   77    18      > JMP                                                      ->20
   81    19    >   ECHO                                                     'Sorry%2C+can%27t+find+the+ID%21'
   83    20    >   ECHO                                                     '%3C%2Fpre%3E'
         21      > RETURN                                                   1

Function findobject:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 47
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 21
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 46) Position 1 = 28, Position 2 = 32
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 47
Branch analysis from position: 33
2 jumps found. (Code = 77) Position 1 = 40, Position 2 = 46
Branch analysis from position: 40
2 jumps found. (Code = 78) Position 1 = 41, Position 2 = 46
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
Branch analysis from position: 46
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 46
Branch analysis from position: 47
Branch analysis from position: 32
Branch analysis from position: 47
filename:       /in/rWkPq
function name:  findObject
number of ops:  48
compiled vars:  !0 = $id, !1 = $obj, !2 = $myObj, !3 = $child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   52     2        BIND_GLOBAL                                              !2, 'myObj'
   54     3        ISSET_ISEMPTY_DIM_OBJ                         0          !1, 'id'
          4      > JMPZ                                                     ~4, ->47
   55     5    >   ROPE_INIT                                     3  ~7      'Checking+'
          6        FETCH_DIM_R                                      ~5      !1, 'id'
          7        ROPE_ADD                                      1  ~7      ~7, ~5
          8        ROPE_END                                      2  ~6      ~7, '%3Cbr+%2F%3E'
          9        ECHO                                                     ~6
   57    10        FETCH_DIM_R                                      ~9      !1, 'id'
         11        IS_EQUAL                                                 !0, ~9
         12      > JMPZ                                                     ~10, ->21
   59    13    >   ROPE_INIT                                     3  ~13     '%3Cstrong%3EYay+we+found+'
         14        FETCH_DIM_R                                      ~11     !1, 'id'
         15        ROPE_ADD                                      1  ~13     ~13, ~11
         16        ROPE_END                                      2  ~12     ~13, '%3C%2Fstrong%3E%3Cbr+%2F%3E'
         17        ECHO                                                     ~12
   60    18        ASSIGN                                                   !2, !1
   61    19        ECHO                                                     '%3Cstrong%3ENeed+to+find+a+way+to+break+out%21%3C%2Fstrong%3E%3Cbr+%2F%3E'
   57    20      > JMP                                                      ->47
   64    21    >   ROPE_INIT                                     3  ~18     'Checking+children+of+'
         22        FETCH_DIM_R                                      ~16     !1, 'id'
         23        ROPE_ADD                                      1  ~18     ~18, ~16
         24        ROPE_END                                      2  ~17     ~18, '%3Cbr+%2F%3E'
         25        ECHO                                                     ~17
   66    26        ISSET_ISEMPTY_DIM_OBJ                         0  ~20     !1, 'children'
         27      > JMPZ_EX                                          ~20     ~20, ->32
         28    >   FETCH_DIM_R                                      ~21     !1, 'children'
         29        COUNT                                            ~22     ~21
         30        IS_SMALLER                                       ~23     0, ~22
         31        BOOL                                             ~20     ~23
         32    > > JMPZ                                                     ~20, ->47
   67    33    >   ROPE_INIT                                     3  ~26     'There+are+children+for+'
         34        FETCH_DIM_R                                      ~24     !1, 'id'
         35        ROPE_ADD                                      1  ~26     ~26, ~24
         36        ROPE_END                                      2  ~25     ~26, '%3Cbr+%2F%3E'
         37        ECHO                                                     ~25
   68    38        FETCH_DIM_R                                      ~28     !1, 'children'
         39      > FE_RESET_R                                       $29     ~28, ->46
         40    > > FE_FETCH_R                                               $29, !3, ->46
   69    41    >   INIT_FCALL_BY_NAME                                       'findObject'
         42        SEND_VAR_EX                                              !0
         43        SEND_VAR_EX                                              !3
         44        DO_FCALL                                      0          
   68    45      > JMP                                                      ->40
         46    >   FE_FREE                                                  $29
   74    47    > > RETURN                                                   null

End of function findobject

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.94 ms | 1021 KiB | 15 Q