[eluser]Aken[/eluser]
Keep in mind that if you have duplicate values in your array, end() may not function correctly. If your last element is equal to other elements in the array, that comparison will return true for the duplicates. Run this and you'll see what I mean:
Code:
$array = array('first', 'middle', 'last', 'middle', 'last');
$end = end($array);
foreach ($array as $a)
{
echo $a;
if ($a == $end) echo ' END';
echo '<br>';
}
I prefer the method Mauricio suggested, except his implementation has a small bug. In his example, you'd want to either do $count++
before the comparison, or start $i at 1 instead of 0.
Here's how I would do it:
Code:
$array = array('first', 'middle', 'last', 'middle', 'last');
$count = count($array);
$i = 0;
foreach ($array as $a)
{
echo $a;
if (++$i == $count) echo ' END';
echo '<br>';
}