Puddinq.com sharing knowledge

Output array() as html table with keys as colomn header

Output array() as html table with keys as colomn header

This function returns every array() as html table.

function puddinq_display_table($result) {

    if (is_null($result)) {
        return;
    }
    ?>
    <table>
      <thead>
        <tr>
          <th><?php echo implode('</th><th>', array_keys(current($result))); ?></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($result as $row): array_map('htmlentities', $row); ?>
          <tr>
            <td><?php echo implode('</td><td>', $row); ?></td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
    <?php
}