Site menu

CodeCook.io

  • Languages
    • C
    • C++
    • CSS
    • Git
    • HTML
    • Java
    • JavaScript
    • jQuery
    • PHP
    • Python
    • Ruby
    • Scala
  • array

PHP - Get last element of array

Retrieve the last item in an array of elements.

0
1689
  • Share on Facebook
  • Share on Google+
  • Share on Twitter

Using end

end() advances array's internal pointer to the last element, and returns its value. documentation

end({{array}})
php

Notes

Value or None

last update: Nov. 14, 2014

0
1689
  • Share on Facebook
  • Share on Google+
  • Share on Twitter

Using array_slice

Slice back element of array into new array. The first element is taken of this new one element array.

array_slice({{array}}, -1)[0]
php

Notes

  • array_slice yields array which needs to be indexed
  • Original array is unmodified

last update: Nov. 14, 2014

0
1688
  • Share on Facebook
  • Share on Google+
  • Share on Twitter

Using array_pop

array_pop() pops and returns the last value of the array, shortening the array by one element. If array is empty (or is not an array), NULL will be returned. Will additionally produce a Warning when called on a non-array. documentation

array_pop({{array}})
php

Notes

  • Original array is shortened by 1
Value or NULL

last update: Nov. 14, 2014

Advertisement

This project is hosted on DigitalOcean, try it and support us

Related concept

  • Sort list of numbers
  • Array length
  • Append item to array
  • Concatenate two arrays
  • Get random item from array

Other language

  • Python
  • JavaScript
  • jQuery

Random task

  • Undo git reset hard
  • Run docker daemon container
  • Rename git branch locally
  • Prepend item to array
  • List processes listening on ports
  • Blog
  • About
  • License