This time to bring you PHP iterator interface iterator how to use, PHP iterator interface iterator the use of considerations, the following is the actual case, together to see.
This example describes the PHP iterator interface iterator usage. Share to everyone for your reference, as follows:
The purpose of the PHP iterator interface is to allow the object to iterate through the internal data in its own way so that it can be iterated over, and the iterator interface is summarized as follows:
Iterator extends Traversable { //returns the element that the current index cursor points to, abstract public mixed current (void) //Returns the key that is pointing to the currently indexed cursor Abstract public scalar key (void) //move current index cursor to next element abstract public void next (void) //Reset index cursor abstract p ublic void Rewind (void) //Determines whether the element pointed to by the current index cursor is valid abstract public boolean valid (void)}
The following is a simple example of how iterator is used:
<?php/** * This class allows the outer iteration of its own internal private property $_test and demonstrates the iterative process * * @author crazy old driver */class Testiterator implements Iterator {/* * define the Array */Private $_test = Array (' dog ', ' cat ', ' pig '); /* * INDEX CURSOR */private $_key = 0; /* * Perform steps */private $_step = 0; /** * Points the index cursor to the initial position * * @see testiterator::rewind () */Public Function rewind () {echo '. + + $this->_step. ' Step: Execute '. METHOD. ' <br> '; $this->_key = 0; /** * Determines whether the element pointed to by the current index cursor is set * * @see testiterator::valid () * @return BOOL */Public Function valid () {echo ' Section '. + + $this->_step. ' Step: Execute '. METHOD. ' <br> '; return Isset ($this->_test[$this->_key]); /** * Points the current index to the next position * * @see testiterator::next () */Public function next () {echo '. + + $this->_step. ' Step: Line '. METHOD. ' <br> '; $this->_key++; /** * Returns the value of the element to which the current index cursor is pointing * * @see testiterator::current () * @return value */Public function current () {Ech O '. + + $this->_step. ' Step: Execute '. METHOD. ' <br> '; RetuRN $this->_test[$this->_key]; }/** * Returns the current index value * * @return Key * @see testiterator::key () */Public Function key () {echo ' + + $this _step. ' Step: Execute '. METHOD. ' <br> '; return $this->_key; }} $iterator = new Testiterator (), foreach ($iterator as $key = + $value) {echo "Output index is {$key} element". ": $value". ' <br><br> ';}? >
The above example will output:
1th Step: Execute testiterator::rewind 2nd step: Execute testiterator::valid 3rd Step: Execute testiterator::current 4th step: Execute Testiterator::key Output Index to 0 element :d og 5th step: Execute testiterator::next 6th step: Execute testiterator::valid 7th step: Perform testiterator::current 8th step: Execute Testiterator:: Key output Index is 1 elements: Cat 9th: Execute testiterator::next 10th step: Perform testiterator::valid 11th step: Perform testiterator::current 12th step: Execute Testiterator::key an element with an output index of 2: Pig 13th step: Execute testiterator::next 14th step: Execute Testiterator::valid
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
How PHP removes null or empty elements from an array
PHP Implements a log function