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);
Output for 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0 - 8.5.1
Array ( [222] => Array ( [0] => 1 [1] => 1 ) [223] => Array ( [0] => 1 [1] => 1 [2] => 1 ) )
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
63.37 ms | 407 KiB | 5 Q