<?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 posts
echo "$post,";
}
// close col div
echo "</div>";
}
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
<div>post1,post5,post9,</div><div>post2,post6,</div><div>post3,post7,</div><div>post4,post8,</div>