0

I have a php document, and I would like to display a variable from the last item from an array.

This how I have it:

<?php if(count($items)): ?>
<?php foreach ($items as $key=>$item):?>

    <?php echo $lastone ?>

<?php endforeach; ?>
<?php endif; ?>
Shaz
  • 2,647
  • 2
  • 35
  • 45
stu
  • 9
  • 2

2 Answers2

5

use php end() method http://php.net/manual/en/function.end.php to get last element.

swapnesh
  • 26,318
  • 22
  • 94
  • 126
  • Thanks swapnesh. Is it possible Upside down? I mean show the first? – stu Feb 21 '13 at 19:07
  • @user2096761 please specify it a little bit more...not getting what exactly u want – swapnesh Feb 21 '13 at 19:10
  • To show the first element from the array, you mean? You can use `reset($array)` to get the first item, but you can also just use `$array[0]`. – octern Feb 21 '13 at 19:59
0

$items[0] may not work if your array is assoc array.

simply use array_pop($items) and array_shift($items) to get last and first item respectively. As suggested by previous answer, use end() wherever possible.

Romit
  • 96
  • 2
  • 13