To convert an HTML table into a PHP array in PHP 8.2, you can use the DOMDocument class, which provides a structured way to parse and extract data from HTML. Here’s an example of how to achieve this:
Example Code:
<?php
function htmlTableToArray(string $html): array {
$dom = new DOMDocument();
// Suppress errors due to invalid HTML and load the HTML string
@$dom->loadHTML($html);
$rows = $dom->getElementsByTagName('tr');
$tableArray = [];
foreach ($rows as $row) {
$rowData = [];
$cells = $row->getElementsByTagName('td');
foreach ($cells as $cell) {
$rowData[] = trim($cell->nodeValue);
}
if (!empty($rowData)) {
$tableArray[] = $rowData;
}
}
return $tableArray;
}
// Example HTML input
$html = <<<HTML
<table>
<tr><td>Row 1, Col 1</td><td>Row 1, Col 2</td></tr>
<tr><td>Row 2, Col 1</td><td>Row 2, Col 2</td></tr>
</table>
HTML;
$array = htmlTableToArray($html);
print_r($array);
Output:
Array
(
[0] => Array
(
[0] => Row 1, Col 1
[1] => Row 1, Col 2
)
[1] => Array
(
[0] => Row 2, Col 1
[1] => Row 2, Col 2
)
)
Explanation:
- DOMDocument: The
DOMDocument
class is used to parse the HTML string. It provides robust methods for traversing and extracting HTML elements. - Suppress Errors: Invalid or malformed HTML might throw warnings. Using
@
suppresses these errors during the parsing phase. - Get Rows: The
<tr>
elements are fetched usinggetElementsByTagName('tr')
. - Extract Data: Within each row (
<tr>
), the data cells (<td>
) are fetched, and their text content is added to an array. - Output: The final result is an array representing the rows and columns of the table.
This approach is flexible and can handle more complex HTML tables with nested elements or extra attributes. If your table contains <th>
elements (table headers), you can adapt the function to process them accordingly.