Generate an HTML table from a multidimensional PHP array with keys and values

In this example, we have a multidimensional array with three rows and three columns. We use a foreach loop to iterate over the rows, and for each row we output a new table row () with the corresponding values from the array. We also use short tags to print the values of the array.

 
<?php
 
$data = array(
    array('name' => 'John', 'age' => 25, 'email' => 'john@example.com'),
    array('name' => 'Mary', 'age' => 30, 'email' => 'mary@example.com'),
    array('name' => 'Bob', 'age' => 40, 'email' => 'bob@example.com')
);
?>

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Email</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($data as $row): ?>
            <tr>
                <td> $row['name'] ?></td>
                <td> $row['age'] ?></td>
                <td> $row['email'] ?></td>
            </tr>
        <?php endforeach ?>
    </tbody>
</table>