PHP iterator (iteration)

Source: Internet
Author: User
Tags rewind

Iterator (iteration)
I. The simplest iteration form in PHP is the foreach statement.

Foreach can be used as an array, for example:
Foreach ($ arr as $ key = >$ value) {echo $ key. '. $ value.'/N ';}
Foreach can also be used for objects, such:
Foreach ($ OBJ as $ property) {echo $ property. '/N ';}
Note: If this statement is used outside an object, only public attributes can be output, and private and protected attributes cannot be output.

II. The following examples help you understand iterator. directoryiterator, recursivedirectoryiterator, and limititerator are pre-defined iterator classes in PHP:

Example 1 show all files in the/etc/directory (do not show files in subdirectories ):

View Source Code

Print help

1 $path = new DirectoryIterator(“/etc/”);
2 foreach ($path as $file){
3 echo $file.’/n’;
4 }

Example 2 show all the files in the subdirectories of any layer nested in the/etc/directory (show the directory path ):

View Source Code

Print help

1 $path = new RecursiveDirectoryIterator(“/etc/”);
2 foreach ($path as $file){
3 echo $file->getPathname().’/n’;
4 }

Example 3 shows the first 10 files in the/etc/directory:

View Source Code

Print help

1 $path = new LimitIterator(DirectoryIterator(“/etc/”),0,10);
2 foreach ($path as $file){
3 echo $file.’/n’;
4 }

From these examples, we can see that using iterator, the code is clear and the programming speed is fast.

Iii. Simple Principle

1. classes that implement the five methods in the iterator interface in PHP are called iterator. These five methods are:
Current ()-returns the current element value,
Key ()-return the key value of the current element,
Next ()-Move down an element,
Valid ()-determines whether there are any subsequent elements. If so, true is returned,
Rewind ()-Move to the first element.

Commonly used iterator: directoryiterator, simplexmliterator.

2. Recursive iterator is a class that implements the haschildren () and getchildren () methods in the recursiveiterator interface,
For example, recursivedirectoryiterator in example 2 above.

3. The link of iterator is displayed in Example 3, that is, an iterator (which can be called the source iterator) is used as a parameter of another iterator (which can be called the target iterator), which is usually used for data filtering.
The commonly used iterator for linking is filteriterator and limititerator.
Filteriterator is an abstract class and needs to implement its accept () method as a rule for data filtering,
Limititerator is a class and uses new limititerator (source iterator, start bit, number) to obtain the specified number of elements.

4. The iteratoraggregate interface is provided in PHP to construct non-iterator classes as iterator. Actually, the getiterator () method in this interface is implemented.

 

 

For example:

Class sample implements iterator

{

Private $ _ items = array (1, 2, 3, 4, 5, 6, 7 );

 

Public Function _ construct (){

; // Void

}

Public Function rewind () {reset ($ this-> _ items );}

Public Function current () {Return Current ($ this-> _ items );}

Public Function key () {return key ($ this-> _ items );}

Public Function next () {return next ($ this-> _ items );}

Public Function valid () {return ($ this-> current ()! = False );}

}

 

$ SA = new sample ();

Foreach ($ SA as $ key => $ Val ){

Print $ key. "=>". $ val;

}

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.