From the course: Learning the Standard PHP Library
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Using RecursiveCachingIterator with multidimensional structures - PHP Tutorial
From the course: Learning the Standard PHP Library
Using RecursiveCachingIterator with multidimensional structures
To find the final elements in multidimensional structures you can use the RecursiveCachingIterator class. Like CachingIterator, the hasNext method looks ahead to see if there are any remaining elements. When iterating through child elements, hasNext returns false when the current element has no more siblings, even if there are still more elements at the parents' level. To illustrate how it works, we'll use the code in Recursive_Caching.PHP. You can find this in the Chapter Six folder of the exercise files. It's a copy of the code used in Chapter Four in the video about RecursiveArrayIterator. Let's insert a new line between RecursiveArrayIterator and RecursiveIteratorIterator on Line Eight, and we'll chain "products" to a new RecursiveCachingIterator and in the "else" block down here on Lines 14 to 16, we'll find out if the current element is the last. So, we add in a new line there, "if not", then "products", that's our iterator, hasNext. If it doesn't have another element following…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
(Locked)
Creating a repeat sequence with InfiniteIterator3m 35s
-
(Locked)
Treating the final element differently with CachingIterator4m 14s
-
(Locked)
Using RecursiveCachingIterator with multidimensional structures2m 59s
-
(Locked)
Generating an ASCII graphic tree with RecursiveTreeIterator4m 28s
-
(Locked)
NoRewindIterator and EmptyIterator2m 26s
-
(Locked)
-
-