PHP gets the previous prev and next next subscript value of an array of arbitrary subscript keys
<?PHP$XOOPS[1] = ' small '; $xoops [2] = ' child '; $xoops [3] = ' child '; $xoops [4] = ' gas '; $steps = new Steps (); foreach ($xoops as $key + = $v Alue) {$steps->add ($key);} $steps->setcurrent (3);//parameter is the key value Echo ' previous subscript: '. $steps->getprev (). " <br/> "; Echo ' specified subscript: '. $steps->getcurrent ()." <br/> "; Echo ' next subscript: '. $steps->getnext ()." <br/> "; 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