One: What is an iterator
The process of iterating through a linked list or an element in an array in a uniform way is called iterative traversal, which we call an iterator.
II: Arrayiterator iterators
1:iterator interface
Iterators in PHP are defined by the iterator interface. Including
Methods such as current (), key (), Next (), rewind (), valid ().
2: Method
foreach implements the Arrayiterator interface by default. But we are learning this iterator to achieve more advanced functionality.
For example, we want to skip the first n elements, we can use Seek (n), for example, before the traversal we want to sort the array by key value, you can call Asort (), the key name sort by Ksort ().
$it = new Arrayiterator ($fruits); Arrayiterator iterators
$it->seek (2); Skip the first 2 elements of $fruits
$it->asort (); Array $fruits sorted by key value
$it->ksort (); Sorting an array $fruits by key name
2: Code
1<?PHP2 $fruits=Array (3"Apple" and "Apple value",4"Orange" = "Orange value",5"Grape" = "Grape value",6"Plum" = "Plum value"7 );8 9 foreach($fruits as $k=$v ) {Ten Echo $k. "=".$v. "<br>"; One } A Echo"****use Arrayiterator in Foreach****<br>"; - //here, as with the foreach result above, the foreach above implements this mechanism by default -$it = new Arrayiterator ($fruits);//arrayiterator iterator - foreach($it as $k=$v ) { - Echo $k. "=".$v. "<br>"; - } + Echo"****use Arrayiterator in While****<br>"; - $it-Rewind (); + while($it-valid ()) { A Echo $it-Key() . "=".$it- Current() . "<br>"; at $it-Next (); - } - Echo"****use Arrayiterator in Seek****<br>"; - //advanced features, skipping elements - $it-Rewind (); - //Skip the first few elements in if($it-valid ()) { - while($it-valid ()) { to if($it-Key() = = "Orange") { //skips the 2nd element + $it-Next(); - } the Echo $it-Key() . "=".$it- Current() . "<br>"; * $it-Next (); $ }Panax Notoginseng } - //Skip the first few elements the $it-Rewind (); + if($it-valid ()) { A //Skip the first few elements to use Seek () the $it->seek (2); //Skip top 2 elements + while($it-valid ()) { - Echo $it-Key() . "=".$it- Current() . "<br>"; $ $it-Next (); $ } - } - //sorting function the Echo"****use Arrayiterator in Ksort****<br>"; - //Sort by Key nameWuyi $it-Rewind (); the $it-Ksort(); - if($it-valid ()) { Wu while($it-valid ()) { - Echo $it-Key() . "=".$it- Current() . "<br>"; About $it-Next (); $ } - } - Echo"****use Arrayiterator in Asort****<br>"; - //Sort by key value A $it-Rewind (); + $it-Asort(); the if($it-valid ()) { - while($it-valid ()) { $ Echo $it-Key() . "=".$it- Current() . "<br>"; the $it-Next (); the } the}
Three: Appenditerator iterator
1: The problem: Suppose there are now two arrays to traverse out, but I do not want to pass two times the foreach traversal out, want to traverse out, how to do?
Then, the Appenditerator iterator is ready to use.
2: Code plus comment
1<?PHP2 $arr 1=Array(' A ', ' B ', ' C ');3 $arr 2=Array(' C ', ' d ', ' e ');4 $it 1=NewArrayiterator ($arr 1);//make an array an iterator5 $it 2=NewArrayiterator ($arr 2);//make an array an iterator6 $it=NewAppenditerator ();//Create a composite iterator object7 $it->append ($it 1);//synthesize the above two iterators8 $it->append ($it 2);9 foreach($it as $k=$v){Ten Echo $k." = ".$v." <br> "; One}
Four: Multipleiterator iterator
1: The problem: Existing two arrays, an array is stored in the user's ID, an array is stored in the user's name, and two array of elements one by one corresponding, how to merge it?
At this point, the Multipleiterator iterator comes in handy.
2: Code plus comment
1<?PHP2 $arr 1=Array(' 1 ', ' 2 ', ' 3 ');3 $arr 2=Array(' ERIC ', ' LIO ', ' DAVID ');4 $it 1=NewArrayiterator ($arr 1);//Create two iterators5 $it 2=NewArrayiterator ($arr 2);//Create two iterators6 //aggregated in an associative array7 $it=NewMultipleiterator (MULTIPLEITERATOR::MIT_KEYS_ASSOC);//creates an iterator aggregation object and aggregates it as an associative array8 $it->attachiterator ($it 1, "ID");//The first association name is an ID9 $it->attachiterator ($it 2, "NAME");//The second association name isTen foreach($it as $v){//Association result Traversal One Print_r($v); A } - //Aggregate as an indexed array - $it=Newmultipleiterator (); the $it->attachiterator ($it 1); - $it->attachiterator ($it 2); - foreach($it as $v){ - Print_r($v); +}
V: Fileystemiterator iterator
1: Question: How to implement the LS function in Linux, how to implement the Dir function in Windows?
The dir command in Windows
Two: Code
1<?PHP2Date_default_timezone_set ("PRC");3 $it=NewFilesystemiterator ('. ');//Create a Filesystemiterator iterator4 foreach($it as $v ) {5 printf("%s\t%s\t%8s\t%s\n",6 Date("Y/m/d h:i:s",$v->getmtime ()),//Get creation Time7 $v->isdir ()? " <DIR> ":" ",//gets whether the directory8 Number_format($v->getsize ()),//get the file size9 $v->getfilename ()//Get file nameTen ); One}
SPL Learning Note (3)---iterator