9

array_pop() removes it from the array. end() changes the internal pointer.

Is the only way really some cludge like:

$my_array[array_pop(array_keys($my_array))];

?

helvete
  • 2,455
  • 13
  • 33
  • 37
Joren
  • 9,623
  • 19
  • 63
  • 104

6 Answers6

24

This works:

list($end) = array_slice($array, -1);

array_slice($array, -1) returns an array with just the last element and list() assigns the first element of slice's result to $end.

@Alin Purcaru suggested this one in comments:

$end = current(array_slice($array, -1));

Since PHP 5.4, this works too:

array_slice($array, -1)[0]
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
  • 4
    What about `$end = current(array_slice($array, -1));` instead of the `list` approach for getting the only element of an array? – Alin Purcaru Sep 20 '11 at 19:48
  • Cool thanks. I was hoping there was some function or approach I wasn't finding. – Joren Sep 20 '11 at 21:29
  • 1
    Unfortunately list($end) = array_slice($array, -1); doesn't work with associative arrays. So I use function pop_array_nondestructive( $array ) { return end($array); } – LibraryThingTim Jul 08 '14 at 15:15
2

Erm... what about reset()ting after you use end()?

$lastItem = end($myArr);
reset($myArr);
Alin Purcaru
  • 43,655
  • 12
  • 77
  • 90
  • 1
    because it changes the internal pointer and OP seems to not want to change the internal pointer ? :) – Arnaud Le Blanc Sep 20 '11 at 19:46
  • @arnaud I'm aware that it resets the pointer, but at least it puts it at the beginning which is the default. Your solution would be more appropriate for not resetting it at all. – Alin Purcaru Sep 20 '11 at 19:54
0

Unfortunately

list($end) = array_slice($array, -1);

doesn't work with associative arrays. So I use

function pop_array_nondestructive( $array )
    {
    return end($array);
    }
LibraryThingTim
  • 413
  • 1
  • 4
  • 10
0
<?php
/**
 * Return last element from array without removing that element from array.
 * https://github.com/jdbevan/PHP-Scripts/
 * 
 * @param array $array The array to get the last element from
 * @return mixed False if $array is not an array or an empty array, else the key of the last element of the array.
 */ 
function array_peek($array) {
    if (!is_array($array)) return false;
    if (count($array)<1) return false;

    $last_key = array_pop(array_keys($array));
    return $array[$last_key];
}
?>
Bedder
  • 1
0
  1. Get last key:
$last_key = array_key_last($my_array); //Get the last key of the given array without affecting the internal array pointer.
  1. Get last element:
echo $my_array[$last_key];
helvete
  • 2,455
  • 13
  • 33
  • 37
-5
end($my_array);

I see nothing bad in changing internal pointer. Nobody is using it these days anyway

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 7
    Again with a sarcastic answer without reading the question. You should really stop this. – Alin Purcaru Sep 20 '11 at 19:44
  • you know that's fastest gun problem, lol. still i see no point in the question even after reading it through – Your Common Sense Sep 20 '11 at 19:47
  • @Alin, what's the issue here. Although late to the game, it's a valid answer and point that's it's an underused function. – Jason McCreary Sep 20 '11 at 19:47
  • 2
    @Both of you. Read the question again. You'll see it eventually. – Alin Purcaru Sep 20 '11 at 19:49
  • @Alin, I have. Maybe you should as your answer suggests using `end()` as well. – Jason McCreary Sep 20 '11 at 19:52
  • While trying to see through an asker's question has it's purpose (*although not applicable here*) the `current(array_slice($array, -1))` is exceedingly better for the added fact that you don't get strict standards issues when passing a non-variable argument (*`current(array_slice(get_array(), -1))`*) – Dan Lugg May 15 '13 at 03:15