<?php
$posts = array('post1', 'post2', 'post3', 'post4', 'post5', 'post6', 'post7', 'post8', 'post9');
// one foreach to divide posts
$col = 1;
foreach($posts as $post) {
$cols[$col] = $post;
$col = $col == 4 ? 1 : $col+1;
}
// then, another foreach for the html
foreach($cols as $col) {
// open col div
echo "<div>";
foreach($col as $post) {
// print post
echo "$post,";
}
// close col div
echo "</div>";
}
<div>
Warning: foreach() argument must be of type array|object, string given in /in/nov9k on line 13
</div><div>
Warning: foreach() argument must be of type array|object, string given in /in/nov9k on line 13
</div><div>
Warning: foreach() argument must be of type array|object, string given in /in/nov9k on line 13
</div><div>
Warning: foreach() argument must be of type array|object, string given in /in/nov9k on line 13
</div>
<div>
Warning: Invalid argument supplied for foreach() in /in/nov9k on line 13
</div><div>
Warning: Invalid argument supplied for foreach() in /in/nov9k on line 13
</div><div>
Warning: Invalid argument supplied for foreach() in /in/nov9k on line 13
</div><div>
Warning: Invalid argument supplied for foreach() in /in/nov9k on line 13
</div>