polemios Posted February 27, 2008 Share Posted February 27, 2008 Hi all, I would like to know how to move the pointer to a specific value in an array. I know how to use prev() and next() to navigate once I set the pointer, but don't know how to set the pointer. Basically, I'm using readdir() to make an array out of a folder of images. That populates an index page with thumbnails. Each thumbnail is linked to a viewing page (view.php) that uses a get variable (the image's filename) to pull in the main image. Once I'm in the view page, I want to be able to use the get variable (the image filename) to search through the readdir() array, using in_array(), and set the pointer position. From that point I could use the prev() and next() function to populate variables for my "prev" and "next" links in my viewer. I know this is trying to reinvent the wheel (i.e. gallery page), but I don't know how else to do it, without using session variables, which I'd prefer not to do. -Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/93381-how-to-navigate-inside-of-an-array/ Share on other sites More sharing options...
Lashiec Posted February 27, 2008 Share Posted February 27, 2008 http://us2.php.net/manual/en/function.array-search.php Try the array_search() function instead. It finds the item in the array and returns it's index. Quote Link to comment https://forums.phpfreaks.com/topic/93381-how-to-navigate-inside-of-an-array/#findComment-478360 Share on other sites More sharing options...
polemios Posted February 27, 2008 Author Share Posted February 27, 2008 Great! Thanks. I assume that the key that is found using array_search is then pointed to by the array pointer? If not, is there some way to set the pointer, like set_pointer(2, $array)? (dumb example, but you get the drift) Quote Link to comment https://forums.phpfreaks.com/topic/93381-how-to-navigate-inside-of-an-array/#findComment-478379 Share on other sites More sharing options...
Lashiec Posted February 27, 2008 Share Posted February 27, 2008 It just returns the pointers value. I read over the array functions and didn't see anything to be able to set the internal counter of an array to a specific value. I just saw reset, end, next, prev and so on. But you could do something like $key = array_search('value', $array) and then do echo $array[$key], but that may not be what you're looking for since you're using the internal pointers... Quote Link to comment https://forums.phpfreaks.com/topic/93381-how-to-navigate-inside-of-an-array/#findComment-478449 Share on other sites More sharing options...
cooldude832 Posted February 27, 2008 Share Posted February 27, 2008 array position movement is sorta an outdated method (with the exception of foreach loops) the more practical idea is to get a key and then go to it by saying $arrayname[$key]; Quote Link to comment https://forums.phpfreaks.com/topic/93381-how-to-navigate-inside-of-an-array/#findComment-478454 Share on other sites More sharing options...
polemios Posted February 27, 2008 Author Share Posted February 27, 2008 Thank you very much. I am now finding the current key with array_search, then assigning $prev and $next variables by adding and subtracting 1 from the current key, and working from there. Thanks. Solved. Quote Link to comment https://forums.phpfreaks.com/topic/93381-how-to-navigate-inside-of-an-array/#findComment-478588 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.