WebYou can use the end () function to get the last element in an array, and array_keys () to return an array of the array-keys. Confusing. In practice, it works like this: $key = end (array_keys ($array)); Credit goes to hollsk in the comments. Share Improve this answer Follow edited Jul 18, 2010 at 10:15 answered Jul 18, 2010 at 10:10 Sam152 WebTo find the last item, I find this piece of code works every time: foreach ( $items as $item ) { if ( !next ( $items ) ) { echo 'Last Item'; } } Share Improve this answer edited Mar 23, 2015 at 20:53 Benoit Esnard 2,007 2 24 32 answered Aug 16, 2014 at 17:36 Yojance 1,549 1 9 8 2 This has too few upvotes, is there any drawback to using this?
Get the last element of an array in PHP - Koen Woortman
WebFeb 27, 2013 · How can I get the value of price of the last element in the array? Cheers php multidimensional-array Share Improve this question Follow asked Feb 27, 2013 at 12:54 adnan 1,345 2 15 31 Add a comment 2 Answers Sorted by: 19 Try this : $array is your input array $arr = end ($array); echo $arr ['price']; WebThe W3Schools online code editor allows you to edit code and view the result in your browser heartsbane triad disappearing
Splitting strings in PHP and get the last part - Stack Overflow
WebOct 8, 2012 · Removes the last element from the array, and returns it: array_pop ($array); If you dont mind doing them both at the same time, you can use: array_shift ($array,1,-1)); to knock off the first and last element at the same time. Check the array_push, array_pop and array_slice documentation :) Share Improve this answer Follow WebMar 12, 2024 · For getting the last element of an array in PHP you have multiple options, which function slightly differently. The most obvious ones are end (), array_slice () and … WebGetting the first or last item from an array in PHP is usually quite easy. If you create an array and then add a bunch of values to it then the array index will increment by 1 for … heartsbane sword game of thrones