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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.