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

php scripts anvar in

You can simply find the last array element with array length -1

$var = array("1","2","3","");
$i = 0;
$len = count($var);
foreach ($array as $item) {
    if ($i == 0) {
        // first result
    } else if ($i == $len - 1) {
        // last result
    } 
    // …
    $i++;
}