PHP gets the previous prev and next next subscript value of an array of arbitrary subscript keys

Source: Internet
Author: User

PHP gets the previous prev and next next subscript value of an array of arbitrary subscript keys


&LT;?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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.