php iterators

PHP Iterators Part 2: SPL Array Iterator, Directory Iterator, and Filesystem Iterator

In this part of this series (PHP Iterators) we will continue demonstrating iterators. We will talk about SPL iterators and what helpful features it provide out of the box.

 

 

Series Topics:

 

In the previous part of this series we learned about iterators, what they do, what iterators class hierarchy and how to extend them using the Iterator and IteratorAggregate classes to create a custom iterator, also learned how to loop over these iterator classes with the foreach loop as if it be a normal array. If you need to go back and review the previous part just click this link.

 

SPL Iterators

PHP provides a special built in iterators through the SPL extension called SPL Iterators. These SPL Iterators perform a lot of tasks which can be iterators that can be used for filtering arrays, iterators for manipulating directory and filesystem, iterators for recursive operations, and many more.

 

PHP SPL Iterators:

  • AppendIterator
  • ArrayIterator
  • CachingIterator
  • CallbackFilterIterator
  • DirectoryIterator
  • EmptyIterator
  • FilesystemIterator
  • FilterIterator
  • GlobIterator
  • InfiniteIterator
  • IteratorIterator
  • LimitIterator
  • MultipleIterator
  • NoRewindIterator
  • ParentIterator
  • RecursiveArrayIterator
  • RecursiveCachingIterator
  • RecursiveCallbackFilterIterator
  • RecursiveDirectoryIterator
  • RecursiveFilterIterator
  • RecursiveIteratorIterator
  • RecursiveRegexIterator
  • RecursiveTreeIterator
  • RegexIterator

 

Array Iterator

This iterator allows to unset and modify values and keys while iterating over Arrays and Objects. ArrayIterator can be created by passing an array to the constructor. When you want to iterate over the same array multiple times you need to instantiate ArrayObject and let it create ArrayIterator instances that refer to it either by using foreach or by calling its getIterator() method manually.

ArrayIterator implements several interfaces as shown below:

As shown above ArrayIterator wraps some of the methods that are used to manipulate arrays like sort and serialize.

 

Let’s see an example

 

As shown here if you run the above code it will print the data as if it be an ordinary array. Now if you print_r the arrayIterator object you will see this output:

 

Let’s see another example to demonstrate the ArrayIterator methods:

In the above code we showed some of the ArrayIterator methods to sort arrays and serialize, there are a lot of other methods to use, check the php.net manual for a list of method.

 

Another example using the ArrayObject class:

 

Directory Iterator

The directory iterator become important when you need to manipulate directory structure and reading directory files like reading the image files of certain directory and displaying them in a gallery. This approach is much simpler than the classic approach of reading the files using something like fopen or fread.

 

As shown the DirectoryIterator support a lot of helper methods for manipulating file structure like retrieving the filename, filesize, filepath, whether it’s a directory or not and many more.

 

In this example we read the contents of the current script directory and display the list of files and their data:

As shown above we iterate over the directory iterator object and retrieve all of the file data simply by using the directory iterator methods.

 

This function is helpful to retrieve the file data any directory by passing the directory path:

To use it like this:

 

Filesystem Iterator

The filesystem iterator much like the directory iterator but the difference is thatDirectoryIterator is an extension of SplFileInfowhileFilesystemIterator is an extension of DirectoryIterator and the both implementsIterator , Traversable , SeekableIterator

 

 

This is an example demonstrate the difference between the DirectoryIterator and the FilesystemIterator

 

Conclusion

In this tutorial of this series (PHP Iterators) we started talking about the SPL Iterators, and described three of them with examples.

Share this: