(PHP 5, PHP 7, PHP 8)
Interface to create an external Iterator.
Example #1 Basic usage
<?php
class myData implements IteratorAggregate
{
public function getIterator(): Traversable
{
return new ArrayIterator([
"key one" => "item one",
"key two" => "item two",
"key three" => "item three"
]);
}
}
$obj = new myData();
foreach ($obj as $key => $value) {
var_dump($key, $value);
echo "\n";
}The above example will output something similar to:
string(7) "key one" string(8) "item one" string(7) "key two" string(8) "item two" string(9) "key three" string(10) "item three"