PHPは配列の任意の下付きキーの前のprevと次のnext下付き値を取得する

1356 ワード

PHPは配列の任意の下付きキーの前のprevと次のnext下付き値を取得する
$value){

$steps->add($key);

}

$steps->setCurrent(3);//   key 
echo '     :'.$steps->getPrev()."
"; echo ' :'.$steps->getCurrent()."
"; echo ' :'.$steps->getNext()."
"; class Steps { private $all; private $count; private $curr; function __construct() { $this->count = 0; } function add($step) { $this->count++; $this->all[$this->count] = $step; } 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); } function getCurrent() { return $this->curr; } function getNext() { self::setCurrent($this->curr); return next($this->all); } function getPrev() { self::setCurrent($this->curr); return prev($this->all); } }

印刷結果:
     :2
     :3
     :4