The following Array Iterator works with standard PHP arrays. Please not, that it works only with numerical arrays, and doesn't support associative PHP arrays. The ArrayIterator class implements PHP Iterator Interface.
The ArrayIterator class is tested with PHP Array Iterator Unit Tests.
require_once('ig/Iterator.class.php'); /** * Standard array iterator. * * @author Ivan Georgiev */ class ig_ArrayIterator extends ig_Iterator { var $theArray; var $nextIndex; function ig_ArrayIterator($anArray) { $this->theArray = $anArray; $this->nextIndex = 0; } /** * Checks if there is a next element available from the underlying collection. * * @return boolean Returns true if there an element is avaialble. */ function hasNext() { return $this->nextIndex < count($this->theArray); } /** * Retruns the next available element (reference) from the collection. * * @return Object */ function & next() { return $this->theArray[$this->nextIndex++]; } }
The ArrayIterator class is a concrete class. We provided a Array Iterator PHP Unit Tests for it.
See also: PHP Iterator Inteface, PHP Collection Interface