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.

Generating an ASCII graphic tree with RecursiveTreeIterator

Generating an ASCII graphic tree with RecursiveTreeIterator - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

Generating an ASCII graphic tree with RecursiveTreeIterator

- The RecursiveTreeIterator class simply iterates over another recursive iterator to generate an ASCII graphic tree of the elements it contains. This is recursivetree.php, which you can find in the chapter six folder of the exercise files. It contains an instance of recursive directory iterator that traverses the common directory, and its sub-directories. To create a graphic tree, just chain RecursiveTreeIterator and then create a loop to display the values. There's no need to use the RecursiveIterator iterator because RecursiveTreeIterator does is all for you. So let's create that here. Files new RecursiveTreeIterator, and we pass it a RecursiveIterator which is called files, and then we just loop over it, foreach files as file, and then inside that loop we echo file and have a line break. Our base iterator is RecursiveDirectoryIterator, so inside this loop, file will be the relative path to each file. So we just save that and then load it into a browser, and that creates the graphic…

Contents