From the course: Learning the Standard PHP Library

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Using RecursiveCachingIterator with multidimensional structures

Using RecursiveCachingIterator with multidimensional structures - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

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…

Contents