What are some best practices for organizing and structuring data extracted from HTML tables using PHP?

When extracting data from HTML tables using PHP, it is essential to organize and structure the data efficiently for further processing or display. One best practice is to use multidimensional arrays to store the table data, with each row represented as an array within the main array. This allows for easy access and manipulation of the data. Additionally, using associative arrays to store column headers as keys can provide a clear structure for the extracted data.

// Sample HTML table data extraction and organization
$html = file_get_contents('example.html');
$dom = new DOMDocument();
$dom->loadHTML($html);

$table = $dom->getElementsByTagName('table')->item(0);
$rows = $table->getElementsByTagName('tr');

$data = array();

foreach ($rows as $row) {
    $rowData = array();
    $cells = $row->getElementsByTagName('td');
    
    foreach ($cells as $cell) {
        $rowData[] = $cell->nodeValue;
    }
    
    $data[] = $rowData;
}

print_r($data);