tibberous Posted December 13, 2009 Share Posted December 13, 2009 I have an array, and I need to know what the current index is. So, lets say I do: $arr = array(); $arr[] = 'one'; $arr[] = 'two'; $arr[] = 'three'; I next index would be 3. I can find that by going count($arr). But, what if I unset an element in $arr? Then count would not return 3. Any idea how to find what the next available spot is? Link to comment https://forums.phpfreaks.com/topic/185010-need-to-get-the-last-index-number-of-an-array/ Share on other sites More sharing options...
Daniel0 Posted December 13, 2009 Share Posted December 13, 2009 $keys = array_keys($array); $next = $keys[count($keys) - 1] + 1; Link to comment https://forums.phpfreaks.com/topic/185010-need-to-get-the-last-index-number-of-an-array/#findComment-976615 Share on other sites More sharing options...
tibberous Posted December 13, 2009 Author Share Posted December 13, 2009 Thanks Link to comment https://forums.phpfreaks.com/topic/185010-need-to-get-the-last-index-number-of-an-array/#findComment-976616 Share on other sites More sharing options...
Alex Posted December 13, 2009 Share Posted December 13, 2009 Or $next = end(array_keys($arr)) + 1; This will not work as you want if the element that you unset is the last one though (I assume this isn't a problem for what you're doing), same with Daniel's. Link to comment https://forums.phpfreaks.com/topic/185010-need-to-get-the-last-index-number-of-an-array/#findComment-976620 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.