tags:

views:

177

answers:

3

Ok,

I know all about array_pop(), but that deletes the last element. What's the best way to get the last element of an array without deleting it?

EDIT: Here's a bonus:

$array = array('a' => 'a', 'b' => 'b', 'c' => 'c');

or even

$array = array('a', 'b', 'c', 'd');
unset($array[2]);
echo $array[sizeof($array) - 1]; // Output: PHP Notice:  Undefined offset:  2 in - on line 4
+7  A: 

Try

$myLastElement = end($yourArray);

To reset it (thanks @hopeseekr):

 reset($array);

Link to manual

Iznogood
Awesome! Obviously I know about the manual (as i linked to it in my question) but it's a LOT of functions to parse and I missed that one. Thanks, bro.
hopeseekr
@hopeseekr I linked the manual so you could read its entry for end() not because I taught you did not know about it :) And it helps others who will search here for the same question. Happy to have helped!
Iznogood
The problem with end($array) is that it modifies the internal pointer of the array to point to the last element, which could be an issue, depending on your implementation details.
Wade Tandy
end($array); reset($array); problem solved ;-)
hopeseekr
The second answer totally doesn't work for key-based arraymaps. In fact, there's no good way except end() as far as I know. I've rescinded the acceptance of this answer until you remove the second part of your answer.
hopeseekr
@hopeseekr edited one last time my answer. Anyways I always end() myself so that answer reflex more what I think. Hey thanks!!
Iznogood
+1  A: 

In almost every language with arrays you can't really go wrong with A[A.size-1]. I can't think of an example of a language with 1 based arrays (as opposed to zero based).

Gabriel
This may not work in PHP, as PHP arrays are more similar to hashes. Consider `unset($a[3])`. Now $a's indices are `0,1,2,4,5` and `$a[count($a) - 1]` yields index 4, not 5. It gets even worse if you have non-numeric keys...
meagar
You can go wrong. Say you unset a middle element, PHP does not reindex the remaining elements. For example, the following code will produce an Undefined offset notice: `$arr = array('a', 'b', 'c'); unset($arr[1]); echo $arr[count($arr)-1];`
webbiedave
@meagar: jinx!!
webbiedave
@webbiedave My god man! Stop following me!
meagar
Cobol, Fortran, Lua, and Smalltalk use 1-based arrays...
BlueRaja - Danny Pflughoeft
VB uses arrays based on 1 by default, tho this can be changed ;-/
hopeseekr
@hopeseeker: This is only true in ancient VB (<= 6).
webbiedave
wow, that's enlightening. PHP's implementation of arrays probably uses a linked list under the hood, I'd guess.
Gabriel
@webbiedave you mean the last one i used? :/
hopeseekr
@gabriel actually it's a hashmap under the hood.
hopeseekr
A: 

As @Gabriel points out:

$lastElement = $theArray[count($theArray) - 1];

// or

$lastElement = $theArray[sizeof($theArray) - 1];

But @Iznogood's solution appears to be the better way (if you are okay with moving the internal array pointer to the end). If you are going to subsequently loop over the array using a foreach, you would need to first call reset($theArray); to set the pointer back to the beginning.

karim79
I added your answer to mine so its complete. +1 to you and Gabriel.
Iznogood
-1 cuz this won't work in the two scenarios I added to the question and is very unsafe.
hopeseekr
If you going to subsequently loop over the array using a foreach, you wouldn't need to first call reset(); that's done automatically. For other iteration methods, you would.
GZipp