Find the last element of an array while using a foreach loop in PHP

It sounds like you want something like this:

$numItems = count($arr);
$i = 0;
foreach($arr as $key=>$value) {
  if(++$i === $numItems) {
    echo "last index!";
  }
}    

That being said, you don’t -have- to iterate over an “array” using foreach in php.

Leave a Comment