Transpose and flatten two-dimensional indexed array where rows may not be of equal length

$data = array(
    0 => array(
        0 => '1',
        1 => 'a',
        2 => '3',
        3 => 'c',
    ),
    1 => array(
        0 => '2',
        1 => 'b',
    ),
);

$newArray = array();
$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
$mi->attachIterator(new ArrayIterator($data[0]));
$mi->attachIterator(new ArrayIterator($data[1]));
foreach($mi as $details) {
    $newArray = array_merge(
        $newArray,
        array_filter($details)
    );
}
var_dump($newArray);

Leave a Comment