$cities = array('London', 'NY', 'LE', 'Rome');
function wrapRow( $row) { return '<div>' . join(", ", $row) . '</div>'; }
$rows = array_chunk( $cities, 3);
$html = join( array_map("wrapRow", $rows));
var_dump($html); // "<div>London, NY, LE</div><div>Rome</div>"
<?php
function group_by_number($arr, $number) {
$res = [];
$counter = 0;
foreach ($arr as $item) {
if ($counter % $number === 0) {
$res []= [];
}
$res[ count($res) - 1] []= $item;
$counter++;
}
return $res;
}
$arr = ['a','b','c','d','e','f','g','h'];
$groupped = group_by_number($arr, 3);
foreach ($groupped as $group) {
?><div class="group"><?php
foreach ($group as $item) {
?><span><?= $item ?></span><?php
}
?></div><?php
}