How can I get the last element of an array without changing its internal pointer?
What i’m doing is:
while(list($key, $value) = each($array)) {
//do stuff
if($condition) {
//Here I want to check if this is the last element of the array
prev($array);
}
}
so end($array) would mess things up.
Warning: this accepted answer is outdated and now triggers the following error. The PHP manual for the
end()specifically says that function’s parameter is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.It’s simple, you could use: