- simplexml_load_string: documentation ( source)
- print_r: documentation ( source)
- iterator_to_array: documentation ( source)
<?php
$xml = simplexml_load_string(<<<EOD
<x>
<ml>1</ml>
<ml>2</ml>
<ml>3</ml>
<ml>4</ml>
<ml>5</ml>
</x>
EOD
);
$gen = function() use ($xml) {
$it = new IteratorIterator($xml->ml);
$it->rewind();
$norewit = new NoRewindIterator($it);
$count = 0;
foreach ($norewit as $key => $value)
{
$count++;
yield $key => "[$value]";
if ($count < 2) {
continue;
}
$norewit->next();
yield from $norewit;
}
};
print_r(iterator_to_array($gen(), false));