Php-iterator interface

Source: Internet
Author: User
Tags rewind
An interface for an external iterator or class that can iterate internally.

Interface Summary

Iterator extends Traversable {/    * method */    Abstract public mixed current (void) abstract public    scalar key (VO ID) abstract public    void Next (void),    abstract public void rewind (void),    abstract Public, Boolean valid (V OID)}

Pre-defined Iterators

PHP has provided some iterators for everyday tasks, such as SPL iterators.

Example

Example #1 Basic Usage

This example shows the sequence of calls to the Iterator method when using foreach.

<?php class Myiterator implements Iterator {private $position = 0;        Private $array = Array ("Firstelement", "Secondelement", "lastelement",);        Public Function __construct () {$this->position = 0;            } function Rewind () {var_dump (__method__);        $this->position = 0;            } function current () {var_dump (__method__);        return $this->array[$this->position];            } function key () {var_dump (__method__);        return $this->position;            } function Next () {var_dump (__method__);        + + $this->position;            } function valid () {var_dump (__method__);        return Isset ($this->array[$this->position]);    }} $it = new Myiterator;        foreach ($it as $key = + $value) {var_dump ($key, $value);    echo "\ n"; }?>

The output of the above routines is similar to the following:

String "Myiterator::rewind" string (myiterator::valid) "Myiterator::current" string (15) " Myiterator::key "Int (0) string ()" Firstelement "string (+)" Myiterator::next "string (" Myiterator::valid "string ( "Myiterator::current" string ("Myiterator::key" int (1) "Secondelement" string () "Myiterator::next" String (2) "Myiterator::valid" string (11) "Myiterator::current" string ("Myiterator::key" Lastelement "string (myiterator::next)" string "Myiterator::valid"

Method List

iterator::current-returns the current element

iterator::key-returns the key of the current element

Iterator::next-move forward to the next element

Iterator::rewind-returns to the first element of an iterator

iterator::valid-Check if the current location is valid

  • Related Article

    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.