3

I have an array with key => value and I want to receive the previous and next entry for a given array key.

Example:

$array = array(
    'one'   => 'first',
    'two'   => 'second',
    'three' => '3rd',
    'four'  => '4th'
)

When I have the given key 'two' I want to receive the entries $array['one'] and $array['three'], is there any nice none foreach solution?

3
  • What would you expect to get if you searched for 'one'? Commented Mar 21, 2011 at 10:38
  • I have the $array and I want to get the prev and next entry for the array of the key "two". Commented Mar 21, 2011 at 10:40
  • I have to search in the prev and next entry if there is a special char in the text. Commented Mar 21, 2011 at 11:52

4 Answers 4

3

The two functions you are looking for are next() and prev(). Native PHP functions for doing exactly what you are after:

$previousPage = prev($array);
$nextPage = next($array);

These functions move the internal pointer so, for example if you are on $array['two'] and use prev($array) then you are now on $array['one']. What I'm getting at is if you need to get one and three then you need to call next() twice.

Sign up to request clarification or add additional context in comments.

1 Comment

How can I be "on $array['two']"?
1
$array = array(
    'one'   => 'first',
    'two'   => 'second',
    'three' => '3rd',
    'four'  => '4th'
);


function getPrevNext($haystack,$needle) {
    $prev = $next = null;

    $aKeys = array_keys($haystack);
    $k = array_search($needle,$aKeys);
    if ($k !== false) {
        if ($k > 0)
            $prev = array($aKeys[$k-1] => $haystack[$aKeys[$k-1]]);
        if ($k < count($aKeys)-1)
            $next = array($aKeys[$k+1] => $haystack[$aKeys[$k+1]]);
    }
    return array($prev,$next);
}

var_dump(getPrevNext($array,'two'));

var_dump(getPrevNext($array,'one'));

var_dump(getPrevNext($array,'four'));

1 Comment

Cool :) A solution without foreach()!
0

You can try to whip something up with an implementation of a SPL CachingIterator.

Comments

0

You could define your own class that handles basic array operations:

Here is an example posted by adityabhai [at] gmail com [Aditya Bhatt] 09-May-2008 12:14 on php.net

<?php
class Steps {

    private $all;
    private $count;
    private $curr;

    public function __construct () {

      $this->count = 0;

    }

    public function add ($step) {

      $this->count++;
      $this->all[$this->count] = $step;

    }

    public function setCurrent ($step) {

      reset($this->all);
      for ($i=1; $i<=$this->count; $i++) {
        if ($this->all[$i]==$step) break;
        next($this->all);
      }
      $this->curr = current($this->all);

    }

    public function getCurrent () {

      return $this->curr;

    }

    public function getNext () {

      self::setCurrent($this->curr);
      return next($this->all);

    }

    public function getPrev () {

      self::setCurrent($this->curr);
      return prev($this->all);

    }

  }
?>

Demo Example:

<?php
   $steps = new Steps();
   $steps->add('1');
   $steps->add('2');
   $steps->add('3');
   $steps->add('4');
   $steps->add('5');
   $steps->add('6');
   $steps->setCurrent('4');
   echo $steps->getCurrent()."<br />";
   echo $steps->getNext()."<br />";
   echo $steps->getPrev()."<br />";
   $steps->setCurrent('2');
   echo $steps->getCurrent()."<br />";
   echo $steps->getNext()."<br />";
   echo $steps->getPrev()."<br />";
?>

1 Comment

It's also a solution with for(each)... :-(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.