How to make a table from PHP array?

The easiest way to create a table with data from a PHP array is to loop through it, with, for example, a foreach() loop.


Use a foreach() loop to put the array’s data inside an HTML table.


// sample data:

$items = [
    'Phone' => '299.99',
    'Laptop' => '699.99',
    'Hammer' => '4.99',


        <th>Item name</th>
        <th>Item price</th>

        <?php foreach($items as $item => $price) { ?>
                <td><?php echo $item; ?></td>
                <td><?php echo $price; ?></td>
        <?php } ?>


Item name Item price
Phone 299.99
Laptop 699.99
Hammer 4.99