How can the data from a database query be efficiently processed and displayed in a table format using PHP arrays?

To efficiently process and display data from a database query in a table format using PHP arrays, you can fetch the data from the database using a query, store the results in an array, and then iterate over the array to generate the table HTML structure. This allows for easy manipulation and customization of the data before displaying it to the user.

<?php
// Assuming $result is the result of a database query

// Fetch data from the database query
$data = [];
while ($row = $result->fetch_assoc()) {
    $data[] = $row;
}

// Display data in a table format
echo '<table>';
echo '<tr>';
foreach (array_keys($data[0]) as $header) {
    echo '<th>' . $header . '</th>';
}
echo '</tr>';

foreach ($data as $row) {
    echo '<tr>';
    foreach ($row as $value) {
        echo '<td>' . $value . '</td>';
    }
    echo '</tr>';
}

echo '</table>';
?>