我有一个 foreach 循环,我需要确定单个项目的位置。项目总是 9。我使用这个代码。有人有更优雅的解决方案吗?
<?php
foreach( $fruit as $key => $apple );
?>
<li class="<?php if( ($key == 0) || ($key == 3) || ($key == 6) ) echo 'first'; if( ($key == 1) || ($key == 4) || ($key == 7) ) echo 'middle'; if( ($key == 2) || ($key == 5) || ($key == 8) ) echo 'last'; ?>"
//stuff
</li>
<?php endforeach; ?>