I just tried creating an iterator that used:
public function ¤t() { $element = &$this->array[$this->position]; return $element; }
But that still didn't work.
The best I can recommend is to implement \ArrayAccess , which will allow you to do this:
foreach ($flavors as $key => $flavor) { $flavors[$key] = $flavor->stdClassForApi(); }
Using generators:
An update based on Marks comments for generators, the following will allow you to iterate over the results without having to implement \Iterator or \ArrayAccess .
class PaginatedResultSet { public $entries = array(); public function &iterate() { foreach ($this->entries as &$v) { yield $v; } } } $flavors = new PaginatedResultSet(); foreach ($flavors->iterate() as &$flavor) { $flavor = $flavor->stdClassForApi(); }
This is a feature available in PHP 5.5.
source share