Foreach loop - three columns(dynamic) table layout <p>i have the below code, which will create a table with three columns.</p> <pre><code>&lt;?php $t = array(' 1 ' , ' 2 ' ,' 3 ', ' 4 ' , ' 5 '); $count = 0; $col =3; echo '&lt;table&gt;&lt;tr&gt;'; foreach($t AS $r){ $count++; echo '&lt;td&gt; '.$r.' &lt;/td&gt;'; if($count == $col){ echo '&lt;/tr&gt;&lt;tr&gt;'; $count = 0; } } echo '&lt;/tr&gt;&lt;/table&gt;'; ?&gt; </code></pre> <p>but what i really want is for the first column to have at least 4 rows before creating a new column and so on. max number of columns is 3 and each column should have at least 4 rows.</p>