Use the module operator. I see that @Alex beat me to such an extent, but I suggest this code that I wrote and tested so that others can more clearly see the principle:
$blogusers=array('a','b','c','d','e','f','g','h','i','j');
$i=0;
foreach ($blogusers as $bloguser) {
if($i % 4 === 0) $extraclass= "fourthClass";
$resultHTML .= "<div class=\"standardClass $extraclass\">$bloguser</div>";
$i++;
$extraclass="";
}
echo $resultHTML;
It can be made more compact with a ternary operator, but that’s the principle.
source
share