How to create a HTML table from PHP array

1 Answer

0 votes
<?php
$s = array(
        array("title" => "aaa", "price" => 65.50),
        array("title" => "bbb", "price" => 923.00),
        array("title" => "ccc", "price" => 87.99),
        array("title" => "ddd", "price" => 7.35)
          );
?>
<?php if (count($s) > 0): ?>
    <table border=1 cellspacing=0 cellpading=0>
        <thead>
            <tr>
                <th><?php echo implode('</th><th>', array_keys(current($s))); ?></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($s as $row): array_map('htmlentities', $row); ?>
                <tr>
                    <td><?php echo implode('</td><td>', $row); ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php endif; ?>

 



answered Feb 23, 2019 by avibootz
edited Feb 23, 2019 by avibootz

Related questions

1 answer 238 views
1 answer 396 views
1 answer 189 views
1 answer 548 views
1 answer 169 views
169 views asked Dec 13, 2018 by avibootz
1 answer 152 views
152 views asked Dec 12, 2018 by avibootz
...