Jump to content

find highest numerical index of an array


pakenney38

Recommended Posts

<?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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.