PHP gets the previous prev and next next subscript value of an array of arbitrary subscript keys
PHP gets the previous prev and next next subscript value of an array of arbitrary subscript keys
$value) {$steps->add ($key);} $steps->setcurrent (3);//parameter is the key value Echo ' previous subscript: '. $steps->getprev (). "
"; Echo ' specifies the subscript: '. $steps->getcurrent ()."
"; Echo ' next subscript: '. $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); }}
Printing results:
Previous subscript: 2 subscript specified: 3 Next subscript: 4