- Let's take a quick look at two unusual iterators:…NoRewindIterator and EmptyIterator.…This is norewind.php, which you can find…in the chapter six folder of the exercise files.…It contains an ArrayIterator and…two foreach loops to display the values.…If we load this into a browser,…the contents of the ArrayIterator are displayed twice.…There's no need to rewind the iterator…before using the second loop…because foreach calls it automatically.…
However, let's see what happens…when we chain our ArrayIterator…with an instance of NoRewindIterator.…So we'll add a new line on line three,…and $sports, new NoRewindIterator,…we'll pass it our ArrayIterator,…save the page and refresh.…This time the first loop works,…but the second one has no values.…Once an instance of NoRewindIterator gets…to the end of the data, it can't be rewound.…
But what happens if we try to rewind it explicitly…by calling the rewind() method?…Let's try that before the second loop.…So $sports, and we invoke the rewind() method,…if we save that and refresh the browser, it's still empty.…
Author
Released
10/22/2014- Using SPL iterators
- Inspecting directories
- Working with text files
- Exporting and extracting data
- Filtering data
- Converting between arrays and iterators
- Combining and merging iterators
- Creating a repeat sequence with InfiniteIterator
- Working with data structures
Skill Level Intermediate
Duration
Views
Related Courses
-
Advanced PHP: Debugging Techniques
with Jon Peck1h 29m Intermediate -
PHP: Exporting Data to Files
with David Powers3h 42m Intermediate
-
Introduction
-
Welcome59s
-
-
1. Introducing the Standard PHP Library (SPL)
-
Introducing SPL iterators1m 59s
-
Using SPL iterators2m 6s
-
2. Exploring Files and Directories
-
3. Filtering Values
-
4. Array Iterators
-
Filtering values from JSON4m 45s
-
5. Combining and Merging Iterators
-
6. Other Iterators
-
7. SPL Data Structures
-
Using SplStack and SplQueue6m 25s
-
Understanding heaps2m 55s
-
Conclusion
-
Goodbye1m 37s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: NoRewindIterator and EmptyIterator