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 ParentIterator to find elements that have children

Using ParentIterator to find elements that have children - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

Using ParentIterator to find elements that have children

- ParentIterator is a specialize filter that selects on elements that have children. It can be useful for building menus or lists of top-level categories. This is parent.php, which you can find in the Chapter 3 folder of the exercise files. It simply contains an instance of recursive directory Iterator that starts at the current directory. So it will traverse the whole file system in the site and then it loops through it. So let's chain this with ParentIterator to filter out elements that don't have children. In other words, we're going to select only the directories. So, add a new line on line 3 and then its new ParentIterator and we pass it our Iterator. And then we need to chain that with RecursiveIterator Iterator. Now, there is actually a problem with this code, but I want to save the page first and load it into a browser to see what happens because it's such an easy mistake to make. So let's load that. The page is completely blank, but we've got no error messages. So let's go…

Contents