gibigbig Posted May 28, 2010 Share Posted May 28, 2010 I have an array of image id's: $all_images = array("33","44","46","100","120","600","907","1200","1239","1299","1305"); as an example and i have the current id of the page from the $_GET['imageid'], as so: $imageid = $_GET['imageid']; what i would like to do is to somehow find a way to see choose the imageid in $all_images directly AFTER the $imageid value. for example, if the $imageid = 600; then i would like to find a way to get the id right after this, "907" (a "next" button) any idea how i can do this? Link to comment https://forums.phpfreaks.com/topic/203234-array-issue/ Share on other sites More sharing options...
jdavidbakr Posted May 28, 2010 Share Posted May 28, 2010 Try this: reset($counter); $nextimage = null; do { if (current($counter) == $_GET['imageid']) { if (next($counter)) { $nextimage = current($counter); } else { // At the end of the array } break; } } while (next($counter)); if ($nextimage) { // We found the next image } else { // Either the image we received isn't in the array or we're at the end of the array } Link to comment https://forums.phpfreaks.com/topic/203234-array-issue/#findComment-1064823 Share on other sites More sharing options...
Cagecrawler Posted May 28, 2010 Share Posted May 28, 2010 This should work: $all_images[array_search($imageid, $all_images)+1] Link to comment https://forums.phpfreaks.com/topic/203234-array-issue/#findComment-1064825 Share on other sites More sharing options...
jdavidbakr Posted May 28, 2010 Share Posted May 28, 2010 This should work: $all_images[array_search($imageid, $all_images)+1] A-ha, I knew there had to be that function, just couldn't find it Link to comment https://forums.phpfreaks.com/topic/203234-array-issue/#findComment-1064826 Share on other sites More sharing options...
kenrbnsn Posted May 28, 2010 Share Posted May 28, 2010 What happens when $imageid is the last element in the array? You better check for boundary conditions... Ken Link to comment https://forums.phpfreaks.com/topic/203234-array-issue/#findComment-1064827 Share on other sites More sharing options...
gibigbig Posted May 28, 2010 Author Share Posted May 28, 2010 Thank you so much jdavidbakr + rep confirmed working Link to comment https://forums.phpfreaks.com/topic/203234-array-issue/#findComment-1064831 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.