10

How can I check if this key is the last element in an array?

$array = array("a","b","c");

The value "c" would have the key 2. Is there some code like this is_end(2) which returns true or false depending if they key is the last of the array? Is there some kind of while() statement I can use?

tarnfeld
  • 25,992
  • 41
  • 111
  • 146
  • Possible duplicates: http://stackoverflow.com/questions/1892848/return-last-numeric-key-not-value-of-an-array , http://stackoverflow.com/questions/216030/what-is-the-best-method-of-getting-the-key-of-the-last-added-array-item-in-php – Felix Kling Feb 14 '10 at 11:46

5 Answers5

22

You could use end() and key() to get the key at the end of the array.

end($array);
$lastKey = key($array);
Yacoby
  • 54,544
  • 15
  • 116
  • 120
  • maybe you mean something like: end($array);$lastKey = key($array); but you cant do it all in just 1 sentence..... – useless Feb 14 '10 at 11:52
  • does this method have any advantages over using `count($array)-1`? – andyface Dec 07 '12 at 10:34
  • 1
    @andyface - the advantage is that you can use this method with associative arrays instead of just numeric based keys (ie: ($array['mykey'] - 1) = undefined) – Jonathan Crowe Dec 16 '12 at 00:14
  • 1
    no better method with associative arrays? this seems clunky? – fraxture Mar 14 '14 at 11:11
  • For the sake of completeness: count() returns the number of items not valid Keys. If you have string keys or other key orders than 0,1,2,3,.. $array[ count($array)-1 ] will not work. – Dennis Heiden Oct 25 '16 at 07:45
5

You can count the array values:

$last_index = count($array) - 1;

But this won't work with associative arrays.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • this is the method I normally use? never sure if it's the best, but the accepted answer seems like it would cause issues/confusion if using with a foreach loop for instance. anyone got any insight? – andyface Dec 07 '12 at 10:33
  • 1
    `foreach` and `end` both modify the internal array pointer (to the "current" element), so using both constructs at the same time messes things up. – Felix Kling Dec 07 '12 at 14:22
  • For the sake of completeness: count() returns the number of items not valid Keys. If you have string keys or other key orders than 0,1,2,3,.. $array[ count($array)-1 ] will not work either. – Dennis Heiden Oct 25 '16 at 07:46
1
$is_2_lastone = array_pop(array_keys($array)) === 2;
useless
  • 1,876
  • 17
  • 18
0

Assuming you don't use an associative array, you could just check the length of the array, using count. It will return 1+last index in array

Marius
  • 57,995
  • 32
  • 132
  • 151
  • For the sake of completeness: count() returns the number of items not valid Keys. If you have string keys or other key orders than 0,1,2,3,.. $array[ count($array)-1 ] will not work. – Dennis Heiden Oct 25 '16 at 07:46
0

If you're using PHP 7 >= 7.3.0 or PHP 8 you can use array_key_last()

$last_key = array_last_key( $arr );

Haddock-san
  • 745
  • 1
  • 12
  • 25