table表示の整形(指定した列数で表示)

<?php
$values = array('A', 'B', 'C', 'D', 'E', 'F', 'G');
$idx = 0;
$col = 3; // カラム数
?>

<table>
<?php foreach ($values as $value) { ?>
  <?php if ($idx % $col == 0) { ?>
  <tr>
  <?php } ?>
    <td><?php echo $value?></td>
  <?php if ($idx % $col == $col - 1) { ?>
  </tr>
  <?php } ?>
  <?php $idx++; ?>
<?php } ?>
<?php if (($idx - 1) % $col != $col - 1) { ?>
  </tr>
<?php } ?>
</table>

[出力結果]
A B C
D E F
G


こちらの方がスマートかも

<?php
$arrays = array('A', 'B', 'C', 'D', 'E', 'F', 'G');
$arrays = array_chunk($arrays, 3); // 折り返したい数で分割
?>

<table>
<?php foreach ($arrays as $array) { ?>
  <tr>
  <?php foreach ($array as $value) { ?>
    <td><?php echo $value ?></td>
  <?php } ?>
  </tr>
<?php } ?>
</table>