Change array's internal pointer?

I’m still very new to using arrays. Looking through the PHP documentation I see how one can advance and rewind the array pointer using the next() and prev() functions.

But I can’t find documentation on how to change the internal pointer to a specific position?? I tried the array_search() for the longest time, attempting to seach for the image name and produce the key, but I just couldn’t pull it off.

I have wasted way too much time on this problem so now I come begging for help.

Lets say I have an array like:

0 - image1.jpg
1 - image2.jpg
2 - image3.jpg
3 - image4.jpg
4 - image5.jpg
5 - image6.jpg

how can i get the pointer to jump to the internal pointer 3 when it is currently at 0? I would like to set the array’s internal pointer using a value stored in a url parameter which is my case is $a.

any ideas?:bawling:

jeff

Do you just want the value of that element? Or do you want to START at that element and move forward from there?

I need to start at the the postion of the element, that way I could move back and forth.

I’m trying to create a photo gallery page, so imagine that you click on the thumbnail and arrive at the page that has the blow-up image. I need to get the array set to the position of that image so that a user could navigate through the enlarged blow up images either back or forward.

You can directly access a array over its key

$test = array();
$test[0] = 2;
$test[1] = 3;
$test[2] = 4;

// accessing the second array element
echo $test[0];

If you do not use sequential index based keys try this:

$test = array();
$test['a'] = 2;
$test['b'] = 3;
$test['c'] = 4;

// store the array keys of $test in a numerical indexed
// array
$keys = array_keys($test);

// access second element
echo $test[$keys[1]];

A final solution would be to use some loop and move the internal array pointer: (assuming that $a holds the info from your GET variable)

$i = 0;
while ($i++ < $a) { next($test); }
// maybe necessary to use prev($test) after the loop

thanks i’ll try this out.

I tried your 3rd suggestion - but my syntax was different and perhaps that was the problem. I’ll start chugging away at this - much appreciated!