There is an often overlooked Iterator that will help you here: The MultipleIterator.
I have created some demo code: https://3v4l.org/beOMV
<?php
$arr = [
0 => [
0 => 'foo',
1 => 'bar',
2 => 'hello',
],
1 => [
0 => 'world',
1 => 'love',
],
2 => [
0 => 'stack',
1 => 'overflow',
2 => 'yep',
3 => 'man',
]
];
$parallelIterator = new MultipleIterator(MultipleIterator::MIT_NEED_ANY|MultipleIterator::MIT_KEYS_NUMERIC);
$parallelIterator->attachIterator(new ArrayIterator($arr[0]));
$parallelIterator->attachIterator(new ArrayIterator($arr[1]));
$parallelIterator->attachIterator(new ArrayIterator($arr[2]));
$result = [];
foreach ($parallelIterator as $values) {
foreach ($values as $value) {
if ($value !== null) {
$result[] = $value;
}
}
}
var_dump($result);
Essentially, iterating over the MultipleIterator will give you an array with all the first entries (and then second and so on) of ALL attached iterators in parallel. By using either MultipleIterator::MIT_NEED_ANY or MultipleIterator::MIT_NEED_ALL you can decide that the loop should stop either when the last iterator has no more elements, or when the first iterator runs out of elements. When you run until the last element of the last iterator, you'll get NULL instead.
When attaching iterators, you can also add a key that will be used in the array when iterating, and have to uses MultipleIterator::MIT_KEYS_ASSOC - I have simply used numeric indices here because the source of the particular array was not interesting.
The result is
array(9) {
[0]=> string(3) "foo"
[1]=> string(5) "world"
[2]=> string(5) "stack"
[3]=> string(3) "bar"
[4]=> string(4) "love"
[5]=> string(8) "overflow"
[6]=> string(5) "hello"
[7]=> string(3) "yep"
[8]=> string(3) "man"
}
Yes, you can iterate over that initial array when attaching the sub arrays to the MultipleIterator if you want:
foreach ($arr as $innerArr) {
$parallelIterator->attachIterator(new ArrayIterator($innerArr));
}