3v4l.org

run code in 300+ PHP versions simultaneously
<?php $resultApi = '<fieldset class="relative"><div><h5><button>CABIN #1</button></h5></div><div>EMPTY</div></fieldset><fieldset class="relative"><div><h5><button>CABIN #2</button></h5></div><div><select data-id="222"><option value="1">Op#1</option><option value="2">Op#2</option></select></div></fieldset><fieldset class="relative"><div><h5><button>CABIN #3</button></h5></div><div><select data-id="223"><option value="1">Op#1</option><option value="2">Op#2</option><option value="3">Op#3</option></select></div></fieldset>'; $dom = new \DOMDocument(); @$dom->loadHTML($resultApi); // Create a DOMXPath instance and locate elements $xpath = new \DOMXPath($dom); $elements = $xpath->query("//fieldset[contains(@class, 'relative')]"); foreach ($elements as $key1 => $element) { $select = $xpath->query("//select", $element); // returns a DOMNodeList $result = []; foreach ($select as $option) { $cabinID =$option->getAttribute('data-id'); $children = $option->childNodes; $parentNode = $xpath->query('//h5/button', $option)->item($key1); foreach ($children as $child) { if($child->nodeName === 'option' && $child->nodeValue !== '-'){ $result/*[$parentNode->nodeValue]*/[$cabinID][]= 1; } } } } print_r($result);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 18, Position 2 = 59
Branch analysis from position: 18
2 jumps found. (Code = 78) Position 1 = 19, Position 2 = 59
Branch analysis from position: 19
2 jumps found. (Code = 77) Position 1 = 27, Position 2 = 57
Branch analysis from position: 27
2 jumps found. (Code = 78) Position 1 = 28, Position 2 = 57
Branch analysis from position: 28
2 jumps found. (Code = 77) Position 1 = 43, Position 2 = 55
Branch analysis from position: 43
2 jumps found. (Code = 78) Position 1 = 44, Position 2 = 55
Branch analysis from position: 44
2 jumps found. (Code = 46) Position 1 = 47, Position 2 = 50
Branch analysis from position: 47
2 jumps found. (Code = 43) Position 1 = 51, Position 2 = 54
Branch analysis from position: 51
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
Branch analysis from position: 54
Branch analysis from position: 50
Branch analysis from position: 55
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 55
Branch analysis from position: 57
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 57
Branch analysis from position: 59
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 59
filename:       /in/cq1JD
function name:  (null)
number of ops:  64
compiled vars:  !0 = $resultApi, !1 = $dom, !2 = $xpath, !3 = $elements, !4 = $element, !5 = $key1, !6 = $select, !7 = $result, !8 = $option, !9 = $cabinID, !10 = $children, !11 = $parentNode, !12 = $child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%3Cfieldset+class%3D%22relative%22%3E%3Cdiv%3E%3Ch5%3E%3Cbutton%3ECABIN+%231%3C%2Fbutton%3E%3C%2Fh5%3E%3C%2Fdiv%3E%3Cdiv%3EEMPTY%3C%2Fdiv%3E%3C%2Ffieldset%3E%3Cfieldset+class%3D%22relative%22%3E%3Cdiv%3E%3Ch5%3E%3Cbutton%3ECABIN+%232%3C%2Fbutton%3E%3C%2Fh5%3E%3C%2Fdiv%3E%3Cdiv%3E%3Cselect+data-id%3D%22222%22%3E%3Coption+value%3D%221%22%3EOp%231%3C%2Foption%3E%3Coption+value%3D%222%22%3EOp%232%3C%2Foption%3E%3C%2Fselect%3E%3C%2Fdiv%3E%3C%2Ffieldset%3E%3Cfieldset+class%3D%22relative%22%3E%3Cdiv%3E%3Ch5%3E%3Cbutton%3ECABIN+%233%3C%2Fbutton%3E%3C%2Fh5%3E%3C%2Fdiv%3E%3Cdiv%3E%3Cselect+data-id%3D%22223%22%3E%3Coption+value%3D%221%22%3EOp%231%3C%2Foption%3E%3Coption+value%3D%222%22%3EOp%232%3C%2Foption%3E%3Coption+value%3D%223%22%3EOp%233%3C%2Foption%3E%3C%2Fselect%3E%3C%2Fdiv%3E%3C%2Ffieldset%3E'
    5     1        NEW                                              $14     'DOMDocument'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $14
    6     4        BEGIN_SILENCE                                    ~17     
          5        INIT_METHOD_CALL                                         !1, 'loadHTML'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0          
          8        END_SILENCE                                              ~17
    9     9        NEW                                              $19     'DOMXPath'
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0          
         12        ASSIGN                                                   !2, $19
   10    13        INIT_METHOD_CALL                                         !2, 'query'
         14        SEND_VAL_EX                                              '%2F%2Ffieldset%5Bcontains%28%40class%2C+%27relative%27%29%5D'
         15        DO_FCALL                                      0  $22     
         16        ASSIGN                                                   !3, $22
   13    17      > FE_RESET_R                                       $24     !3, ->59
         18    > > FE_FETCH_R                                       ~25     $24, !4, ->59
         19    >   ASSIGN                                                   !5, ~25
   14    20        INIT_METHOD_CALL                                         !2, 'query'
         21        SEND_VAL_EX                                              '%2F%2Fselect'
         22        SEND_VAR_EX                                              !4
         23        DO_FCALL                                      0  $27     
         24        ASSIGN                                                   !6, $27
   15    25        ASSIGN                                                   !7, <array>
   16    26      > FE_RESET_R                                       $30     !6, ->57
         27    > > FE_FETCH_R                                               $30, !8, ->57
   17    28    >   INIT_METHOD_CALL                                         !8, 'getAttribute'
         29        SEND_VAL_EX                                              'data-id'
         30        DO_FCALL                                      0  $31     
         31        ASSIGN                                                   !9, $31
   18    32        FETCH_OBJ_R                                      ~33     !8, 'childNodes'
         33        ASSIGN                                                   !10, ~33
   20    34        INIT_METHOD_CALL                                         !2, 'query'
         35        SEND_VAL_EX                                              '%2F%2Fh5%2Fbutton'
         36        SEND_VAR_EX                                              !8
         37        DO_FCALL                                      0  $35     
         38        INIT_METHOD_CALL                                         $35, 'item'
         39        SEND_VAR_EX                                              !5
         40        DO_FCALL                                      0  $36     
         41        ASSIGN                                                   !11, $36
   22    42      > FE_RESET_R                                       $38     !10, ->55
         43    > > FE_FETCH_R                                               $38, !12, ->55
   23    44    >   FETCH_OBJ_R                                      ~39     !12, 'nodeName'
         45        IS_IDENTICAL                                     ~40     ~39, 'option'
         46      > JMPZ_EX                                          ~40     ~40, ->50
         47    >   FETCH_OBJ_R                                      ~41     !12, 'nodeValue'
         48        IS_NOT_IDENTICAL                                 ~42     ~41, '-'
         49        BOOL                                             ~40     ~42
         50    > > JMPZ                                                     ~40, ->54
   24    51    >   FETCH_DIM_W                                      $43     !7, !9
         52        ASSIGN_DIM                                               $43
         53        OP_DATA                                                  1
   22    54    > > JMP                                                      ->43
         55    >   FE_FREE                                                  $38
   16    56      > JMP                                                      ->27
         57    >   FE_FREE                                                  $30
   13    58      > JMP                                                      ->18
         59    >   FE_FREE                                                  $24
   29    60        INIT_FCALL                                               'print_r'
         61        SEND_VAR                                                 !7
         62        DO_ICALL                                                 
         63      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148.96 ms | 1015 KiB | 14 Q