<?php class Test implements Iterator { private $ar = [ 1, 2, 3 ]; public function toArray() { return $this->ar; } public function rewind() { reset( $this->ar ); } public function valid() { return !is_null( $this->key() ); } public function key() { return key( $this->ar ); } public function current() { return current( $this->ar ); } public function next() { next( $this->ar ); } } $t = new Test; foreach( $t as $key => $value ) { echo "orig: $key: $value\n"; foreach( $t->toArray() as $copyKey => $copyValue ) { echo " copy: $copyKey: $copyValue\n"; } echo "----\n"; }
You have javascript disabled. You will not be able to edit any code.