pakenney38 Posted April 2, 2008 Share Posted April 2, 2008 Assume that the first numerical index of a given array is 0. Is there any way to find the highest numerical index of the array? I want to avoid using something like, if I can: $highest_index = count($array) - 1; Quote Link to comment Share on other sites More sharing options...
Caesar Posted April 2, 2008 Share Posted April 2, 2008 <?php krsort($array); //Sort your array in reverse order by key. $highest = key($array); //Get array's current key ksort($array); //Back to normal. ?> Or.... <?php end($array); $highkey = key($array); next($array); ?> I suppose array_keys isn't much help since it just returns another array with keys. We can get those from the original array anyway. It is possible there's a much better way...but it's early and I haven't had my corporate fave Starbucks yet. Quote Link to comment Share on other sites More sharing options...
Cep Posted April 2, 2008 Share Posted April 2, 2008 Hmm, count will give you a count of the elements which may return a number which is not likely to be the highest index number because 1.) Index's can be unset, 2.) Index's might not be numerical If you need the value of the last element you could use the end() function. Quote Link to comment Share on other sites More sharing options...
pakenney38 Posted April 2, 2008 Author Share Posted April 2, 2008 Thanks. That will work. I had done that once before and forgot I could do it that way. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.