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 git.master_jit, git.master
Array ( [222] => Array ( [0] => 1 [1] => 1 ) [223] => Array ( [0] => 1 [1] => 1 [2] => 1 ) )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
47.54 ms | 405 KiB | 5 Q