【问题标题】:PHP key => value array find prev and next entryPHP key => value 数组查找上一个和下一个条目
【发布时间】:2011-07-19 14:02:50
【问题描述】:

我有一个键 => 值的数组,我想接收给定数组键的上一个和下一个条目。

示例:

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

当我拥有给定的密钥'two' 时,我想接收$array['one']$array['three'] 的条目,有什么好的none foreach 解决方案

【问题讨论】:

  • 如果您搜索“one”,您期望得到什么?
  • 我有 $array,我想获取键“two”的数组的上一个和下一个条目。
  • 如果文本中有特殊字符,我必须在上一个和下一个条目中搜索。

标签: php arrays search


【解决方案1】:

您要查找的两个函数是next()prev()。本机 PHP 函数可以完全满足您的需求:

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

这些函数移动内部指针,例如,如果你在 $array['two'] 上并使用 prev($array) 那么你现在在 $array['one'] 上。我的意思是,如果您需要获得 1 和 3,那么您需要调用 next() 两次。

【讨论】:

  • 我怎样才能“在 $array['two']”上?
【解决方案2】:
$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'));

【讨论】:

  • 酷 :) 没有 foreach() 的解决方案!
【解决方案3】:

您可以尝试使用 SPL CachingIterator 的实现来创造一些东西。

【讨论】:

    【解决方案4】:

    您可以定义自己的类来处理基本的数组操作:

    这是 adityabhai [at] gmail com [Aditya Bhatt] 2008 年 5 月 9 日 12:14 在 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 />";
    ?>
    

    【讨论】:

    • 这也是for(each)的解决方案... :-(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 2012-06-07
    • 1970-01-01
    • 2018-09-23
    • 2017-07-11
    相关资源
    最近更新 更多