Flatten array of iterators

385 views Asked by At

I tried different combinations of SPL Iterators, but I can't find a way to implement recursive iteration over array of iterators. Here is analog of what I'm looking for but implemented using Generators:

private function recursiveIterator(): iterable
{
    $iterators = [
        $this->firstIterator(),
        $this->secondIterator(),
    ];

    foreach ($iterators as $iterator) {
        yield from $iterator;
    }
}

Is there any way to write the analog of the above code using SPL Iterators?

Thanks in advance!

P.S. Basically the code flattens array of values

[[1, 2], [10, 20]]

to

[1, 2, 10, 20]
0

There are 0 answers