开发者

PHP key => value array find prev and next entry

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?


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.


$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'));


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


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 />";
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜