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

How is can find the last element of an array while using a foreach loop in PHP
Ankur Rajput
Asked 13-11-2019
37

Answer (1)
$numItems = count($arr);
$i = 0;
foreach($arr as $key=>$value) {
  if(++$i === $numItems) {
    echo "last index!";
  }
} 
Harsh Aggrawal
Asked 29-06-2018
45 Likes
Comments
Write comment

Submit your answer