<?php
class IteratorTest implements ArrayAccess, Iterator {
  private $pointer = 0;
  private $objects = array();

  public function offsetExists($index) {
    return isset($this->objects[$index]);
  }

  public function offsetGet($index) {
    return $this->objects[$index];
  }

  public function offsetSet($index, $newValue) {
    $this->objects[$index] = $newValue;
  }

  public function offsetUnset($index) {
    unset($this->objects[$index]);
  }

  public function key() {
    return key($this->objects);
  }

  public function current() {
    return current($this->objects);
  }

  public function next() {
    next($this->objects);
  }

  public function rewind() {
    reset($this->objects);
  }

  public function valid() {
    return current($this->objects);
  }
}

$it = new IteratorTest();

$it['one'] = 1;
$it['two'] = 2;

foreach ($it as $k => $v) {
  echo "$k: $v\n";
}

// expected result:
// one: 1
// two: 2