Whenever I have to determine if currect loop in foreach cycle is the last one, i use something like this:
<?php
$i = count($myArray);
foreach ($myArray as $item) {
/** code goes here... */
$i--;
if ($i == 0) {
/** something happens here */
}
}
?>
Typically this could by done very easily in templating systems (such as Latte, Smarty, etc...) by knowing the cycle variables (first, last,...). And my question: Is there a similar functionality in PHP?
The key is that you have to create the $i
variable, then increase it and check the value. I just want to know if there exists a simpler solution to make my life a little bit easier. :)