SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    U.S.A
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

    jeff

  2. #2
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you just want the value of that element? Or do you want to START at that element and move forward from there?
    John

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    U.S.A
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can directly access a array over its key

    PHP Code:
    $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:

    PHP Code:
    $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)

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

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    U.S.A
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •