Jump to content

last in the array


glenelkins

Recommended Posts

is there a simple way to tell if my loop on an array is looking at the last record in the array? or is the only way to use a counter?

example code

[code]
foreach ($array as $array2) {
    if ($array2 is the last record) {
        echo "last record";
    } else {
        echo $array2;
    }
}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/9322-last-in-the-array/
Share on other sites

Something like this:
[code]<?php

$array = array("hello", "world", "again");

// loop through array
foreach($array as $key)
{
   // check that the current value $key is equal to the last value in the arrray
    if($key == end($array))
    {
        echo "end of array! - " . $key . "<br />\n"; // end of array
    }
    else
   {
        echo "array is at key " . $key . "<br />\n"; // not end of array
    }
}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/9322-last-in-the-array/#findComment-34355
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.