3v4l.org

run code in 300+ PHP versions simultaneously
<?php $resultset = [ // query just once, collect the full tree ["ID" => 1, "Name" => "News", "Father" => 0], ["ID" => 2, "Name" => "Articles", "Father" => 0], ["ID" => 3, "Name" => "Politics", "Father" => 1], ["ID" => 4, "Name" => "Politics", "Father" => 2], ["ID" => 5, "Name" => "World", "Father" => 3], ["ID" => 6, "Name" => "World", "Father" => 4] ]; $path = "/News/Politics/World"; // $path = "/Articles/The Funnies/Garfield"; // a test case that fails // News: parent = 0, id = 1 // \ // Politics: parent = 1, id = 3 // > the intended logic // World: parent = 3, id = 5 // / $cfg['categories_separator'] = "/"; $breadcrumbs = explode($cfg['categories_separator'], trim($path, "/")); // var_export($breadcrumbs); // see what is generated $parent = 0; // default value foreach ($breadcrumbs as $crumb) { $id = false; // set invalid value for success check foreach ($resultset as $row) { if ($row["Name"] == $crumb && $parent == $row["Father"]) { // qualifying match $id = $parent = $row["ID"]; // dual declaration break; // break inner loop, progress to next $crumb } } if (!$id) { // inner loop failed to find qualifying match echo "Uh-oh, Broken Breadcrumb Path -- $crumb not found in $path\n"; break; // break outer loop, path is invalid } // echo "ID = $id for $crumb\n"; // uncomment to see progress } echo "ID = $id for $crumb\n"; // echo the result
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 43
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 43
Branch analysis from position: 17
2 jumps found. (Code = 77) Position 1 = 19, Position 2 = 32
Branch analysis from position: 19
2 jumps found. (Code = 78) Position 1 = 20, Position 2 = 32
Branch analysis from position: 20
2 jumps found. (Code = 46) Position 1 = 23, Position 2 = 26
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 31
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 42
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
Branch analysis from position: 26
Branch analysis from position: 32
Branch analysis from position: 32
Branch analysis from position: 43
Branch analysis from position: 43
filename:       /in/RGOgX
function name:  (null)
number of ops:  51
compiled vars:  !0 = $resultset, !1 = $path, !2 = $cfg, !3 = $breadcrumbs, !4 = $parent, !5 = $crumb, !6 = $id, !7 = $row
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
   11     1        ASSIGN                                                   !1, '%2FNews%2FPolitics%2FWorld'
   17     2        ASSIGN_DIM                                               !2, 'categories_separator'
          3        OP_DATA                                                  '%2F'
   18     4        INIT_FCALL                                               'explode'
          5        FETCH_DIM_R                                      ~11     !2, 'categories_separator'
          6        SEND_VAL                                                 ~11
          7        INIT_FCALL                                               'trim'
          8        SEND_VAR                                                 !1
          9        SEND_VAL                                                 '%2F'
         10        DO_ICALL                                         $12     
         11        SEND_VAR                                                 $12
         12        DO_ICALL                                         $13     
         13        ASSIGN                                                   !3, $13
   21    14        ASSIGN                                                   !4, 0
   22    15      > FE_RESET_R                                       $16     !3, ->43
         16    > > FE_FETCH_R                                               $16, !5, ->43
   23    17    >   ASSIGN                                                   !6, <false>
   24    18      > FE_RESET_R                                       $18     !0, ->32
         19    > > FE_FETCH_R                                               $18, !7, ->32
   25    20    >   FETCH_DIM_R                                      ~19     !7, 'Name'
         21        IS_EQUAL                                         ~20     !5, ~19
         22      > JMPZ_EX                                          ~20     ~20, ->26
         23    >   FETCH_DIM_R                                      ~21     !7, 'Father'
         24        IS_EQUAL                                         ~22     !4, ~21
         25        BOOL                                             ~20     ~22
         26    > > JMPZ                                                     ~20, ->31
   26    27    >   FETCH_DIM_R                                      ~23     !7, 'ID'
         28        ASSIGN                                           ~24     !4, ~23
         29        ASSIGN                                                   !6, ~24
   27    30      > JMP                                                      ->32
   24    31    > > JMP                                                      ->19
         32    >   FE_FREE                                                  $18
   30    33        BOOL_NOT                                         ~26     !6
         34      > JMPZ                                                     ~26, ->42
   31    35    >   ROPE_INIT                                     5  ~28     'Uh-oh%2C+Broken+Breadcrumb+Path+--+'
         36        ROPE_ADD                                      1  ~28     ~28, !5
         37        ROPE_ADD                                      2  ~28     ~28, '+not+found+in+'
         38        ROPE_ADD                                      3  ~28     ~28, !1
         39        ROPE_END                                      4  ~27     ~28, '%0A'
         40        ECHO                                                     ~27
   32    41      > JMP                                                      ->43
   22    42    > > JMP                                                      ->16
         43    >   FE_FREE                                                  $16
   36    44        ROPE_INIT                                     5  ~32     'ID+%3D+'
         45        ROPE_ADD                                      1  ~32     ~32, !6
         46        ROPE_ADD                                      2  ~32     ~32, '+for+'
         47        ROPE_ADD                                      3  ~32     ~32, !5
         48        ROPE_END                                      4  ~31     ~32, '%0A'
         49        ECHO                                                     ~31
         50      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
138.92 ms | 1405 KiB | 17 Q