3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $html = '<html> <body> <table> <thead> <tr> <th>Name</th> <th>Date</th> </tr> </thead> <tbody> <tr> <td>Chad</td> <td>10/23/1996</td> </tr> <tr> <td>Rebecca</td> <td>05/03/2001</td> </tbody> </table> </body> </html>'; $dom = new DOMDocument(); $dom->loadHTML($html); $headers = $dom->getElementsByTagName('th'); foreach($headers as $header) { $keys[] = strtolower($header->nodeValue); } $rows = $dom->getElementsByTagName('tbody')->item(0)->getElementsByTagName('tr'); $result = []; foreach($rows as $row) { $item = []; foreach($keys as $i => $key) { $cells = $row->getElementsByTagName('td'); $item[$key] = $cells->item($i)->nodeValue; } $result[] = $item; } var_dump($result);
based on v4cQY
Output for 5.6.38 - 7.2.11
array(2) { [0]=> array(2) { ["name"]=> string(4) "Chad" ["date"]=> string(10) "10/23/1996" } [1]=> array(2) { ["name"]=> string(7) "Rebecca" ["date"]=> string(10) "05/03/2001" } }