- var_dump: documentation ( source)
- strtolower: documentation ( source)
<?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);