www.webdeveloper.com
Results 1 to 6 of 6

Thread: Getting the previous and next array key?

  1. #1
    Join Date
    May 2004
    Posts
    9

    Getting the previous and next array key?

    Hello,

    I have an array like this:

    ('car' => 'Ford',
    'computer' => 'Dell',
    'deodorant' => 'Rexona',
    'television' => 'Sony',
    ......
    )

    Assuming that I'm given the key $key='deodorant', what is the easiest way to get the previous key ('computer') and the next key ('television') of the array?

    I know I can do it the hard way, but there must be an easy way to do it, maybe in 1 or 2 lines of coding?
    Ad-Formats.com - listing of the ad networks and their ad formats
    Free Polls & Quizzes - Free Message Board - Free Form Processor

  2. #2
    Join Date
    Sep 2003
    Location
    Portsmouth, UK Occupation: Web Developer
    Posts
    2,602
    Richard Turner - A Virtual Insight

  3. #3
    Join Date
    May 2004
    Posts
    9
    Originally posted by 96turnerri
    next() & prev()
    I don't think that this will work. next() and prev() works with current(), but how do I make the key that I have the "current" key?
    Ad-Formats.com - listing of the ad networks and their ad formats
    Free Polls & Quizzes - Free Message Board - Free Form Processor

  4. #4
    Join Date
    Sep 2003
    Location
    Portsmouth, UK Occupation: Web Developer
    Posts
    2,602
    take the example on php.net

    <?php
    $transport = array('foot', 'bike', 'car', 'plane');
    $mode = current($transport); // $mode = 'foot';
    $mode = next($transport); // $mode = 'bike';
    $mode = next($transport); // $mode = 'car';
    $mode = prev($transport); // $mode = 'bike';
    $mode = end($transport); // $mode = 'plane';
    ?>

    now take your array
    $thing = array('car' => 'Ford', 'computer' => 'Dell', 'deodorant' => 'Rexona', 'television' => 'Sony');
    $item = current($thing); // $item = 'car';
    $item = next($thing); // $item = 'computer';
    $item = next($thing); // $item = 'deodarant';
    //it now knows $item is deodarant
    $item = prev($thing); // $item = 'computer';
    echo "Previous: ".$item."<br>";
    $item = next($thing); // $item = 'deodarant';
    echo "Current: ".$item."<br>";
    $item = next($thing); // $item = 'television';
    echo "Next: ".$item;
    //Will Produce
    //Previous: Computer
    //Current: Deodarant
    //Next: Television
    Richard Turner - A Virtual Insight

  5. #5
    Join Date
    May 2004
    Posts
    9
    Originally posted by 96turnerri
    take the example on php.net

    <?php
    $transport = array('foot', 'bike', 'car', 'plane');
    $mode = current($transport); // $mode = 'foot';
    $mode = next($transport); // $mode = 'bike';
    $mode = next($transport); // $mode = 'car';
    $mode = prev($transport); // $mode = 'bike';
    $mode = end($transport); // $mode = 'plane';
    ?>

    now take your array
    $thing = array('car' => 'Ford', 'computer' => 'Dell', 'deodorant' => 'Rexona', 'television' => 'Sony');
    $item = current($thing); // $item = 'car';
    $item = next($thing); // $item = 'computer';
    $item = next($thing); // $item = 'deodarant';
    //it now knows $item is deodarant
    $item = prev($thing); // $item = 'computer';
    echo "Previous: ".$item."<br>";
    $item = next($thing); // $item = 'deodarant';
    echo "Current: ".$item."<br>";
    $item = next($thing); // $item = 'television';
    echo "Next: ".$item;
    //Will Produce
    //Previous: Computer
    //Current: Deodarant
    //Next: Television
    I still think that this isn't an easy way.

    Suppose that the key is parsed, then you only have the variable $key. You will have to cycle through the array to know where it's position is in the array. Isn't there an easy way?
    Ad-Formats.com - listing of the ad networks and their ad formats
    Free Polls & Quizzes - Free Message Board - Free Form Processor

  6. #6
    Join Date
    Sep 2003
    Location
    Portsmouth, UK Occupation: Web Developer
    Posts
    2,602
    you could use a combination of next, prev and array_search that would do it, please explain in more detail what you are trying to do as im not sure we are talking about the same thing

    Rich
    Richard Turner - A Virtual Insight

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles