<?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);
You have javascript disabled. You will not be able to edit any code.