Using Iterators in PHP | Linux Today

Using Iterators in PHP

Written By
Web Webster
Web Webster
Oct 23, 2009

“The SPL represents a collection of interfaces and classes for
PHP 5 that help resolve some common problems and that let
developers take full advantage of object-oriented programming. Some
of the SPL’s most used capabilities are: advanced array access, the
ability to customize collection iteration with foreach or while,
enhanced file and directory access, and advanced SimpleXML object
handling. But the best new functionality that the SPL provides
comes in the form of iterators.

“According to Wikipedia, an iterator is an object that allows a
programmer to traverse through all the elements of a collection,
regardless of its specific implementation. There are several
different types of iterators, each discussed in more detail
below.

“The ArrayIterator Class

“This iterator allows you to modify values and keys while
iterating over arrays and objects. You can find documentation for
the ArrayIterator methods here. This first example application
iterates recursively over a $numbers array, appends a new value at
the end of the array, and finally prints it to the screen:”

Complete
Story

Web Webster

Web Webster

Web Webster has more than 20 years of writing and editorial experience in the tech sector. He’s written and edited news, demand generation, user-focused, and thought leadership content for business software solutions, consumer tech, and Linux Today, he edits and writes for a portfolio of tech industry news and analysis websites including webopedia.com, and DatabaseJournal.com.

Linux Today Logo

LinuxToday is a trusted, contributor-driven news resource supporting all types of Linux users. Our thriving international community engages with us through social media and frequent content contributions aimed at solving problems ranging from personal computing to enterprise-level IT operations. LinuxToday serves as a home for a community that struggles to find comparable information elsewhere on the web.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.