PHP Iterator interface iterator Usage Analysis, Iterator usage

Source: Internet
Author: User
Tags rewind

PHP Iterator interface iterator Usage Analysis, Iterator usage

This example describes the use of the PHP Iterator interface Iterator. We will share this with you for your reference. The details are as follows:

The PHP Iterator interface allows an object to iterate internal data in its own way so that it can be accessed cyclically. The Iterator interface Abstract:

Iterator extends Traversable {// returns the abstract public mixed current (void) element pointed to by the current index cursor // returns the abstract public scalar key (void) key pointed to by the current index cursor) // Move the current index cursor to the next element abstract public void next (void) // reset the index cursor abstract public void rewind (void) // determine whether the element pointed to by the current index cursor is valid abstract public boolean valid (void )}

The following is a simple example to demonstrate how to use Iterator:

<? Php/*** this class allows external iterations of their internal private attributes $ _ test, and demonstrate the iteration process ** @ author crazy old driver */class TestIterator implements Iterator {/** defines the array to be iterated */private $ _ test = array ('dog ', 'cat', 'pig');/** index cursor */private $ _ key = 0;/** execution step */private $ _ step = 0; /*** direct the index cursor to the initial position ** @ see TestIterator: rewind () */public function rewind () {echo 'quarter '. ++ $ this-> _ step. 'Step: Execute '. _ METHOD __. '<br>'; $ this-> _ key = 0;}/*** determines whether the current index cursor points to an element Set ** @ see TestIterator: valid () * @ return bool */public function valid () {echo 'quarter '. ++ $ this-> _ step. 'Step: Execute '. _ METHOD __. '<br>'; return isset ($ this-> _ test [$ this-> _ key]);} /*** direct the current index to the next position ** @ see TestIterator: next () */public function next () {echo 'quarter '. ++ $ this-> _ step. 'Step: Execute '. _ METHOD __. '<br>'; $ this-> _ key ++;}/*** returns the value of the element pointed to by the current index cursor ** @ see TestIterator: current () * @ return value */public fun Ction current () {echo 'quarter '. ++ $ this-> _ step. 'Step: Execute '. _ METHOD __. '<br>'; return $ this-> _ test [$ this-> _ key];} /*** return the current index value ** @ return key * @ see TestIterator: key () */public function key () {echo 'quarter '. ++ $ this-> _ step. 'Step: Execute '. _ METHOD __. '<br>'; return $ this-> _ key; }}$ iterator = new TestIterator (); foreach ($ iterator as $ key => $ value) {echo "elements whose output index is {$ key ". ": $ value ". '<br>' ;}?>

The above example will be output:

Step 2: Execute TestIterator: rewind Step 2: Execute TestIterator: valid Step 2: Execute TestIterator: current Step 2: Execute TestIterator: key to output elements with an index of 0: dog Step 2: Execute TestIterator: next Step 5th: Execute TestIterator: valid step 6th: Execute TestIterator: current step 7th: Execute TestIterator: key to output the element whose index is 1: cat Step 2: Execute TestIterator: next Step 9th: Execute TestIterator: valid step 10th: Execute TestIterator: current step 11th: Execute TestIterator: key to output 2 elements: pig Step 2: Execute TestIterator: next Step 13th: Execute TestIterator: valid

From the above example, we can see that if the execution of valid returns false, the loop ends.

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.