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



PHP Snippet 1:

$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);

PHP Snippet 2:

$arr1 = [1,'a',3,'c'];
$arr2 = ['2','b'];

$finarry = arrayInterweave($arr1,$arr2);
print_r($finarry);

function arrayInterweave($arr1,$arr2){
  $count1 = count($arr1);
  $count2 = count($arr2);
  $length = (($count1 >= $count2) ? $count1 : $count2);

  $fin = array();
  for($i = 0;$i<$length;$i++){
      if(!empty($arr1[$i])){
        $fin[] = $arr1[$i];
      }
      if(!empty($arr2[$i])){
        $fin[] = $arr2[$i];
      }
  }
  return $fin;
}

PHP Snippet 3:

$array = [
    ["a","b","c"],
    ["d","e"]
];
$result = [];
while($array) { 
    array_walk(
        $array, 
        function(&$subarray, $key) use (&$array, &$result) { 
            $result[] = array_shift($subarray); 
            if(empty($subarray)) unset ($array[$key]); 
        }
    );
}

var_dump($result);

PHP Snippet 4:

$maxCount = max(array_map('count', $array));
$result = [];
for ($i = 0; $i < $maxCount; ++$i) {
    array_push($result, ...array_column($array, $i));
}
var_export($result);

PHP Snippet 5:

$maxCount = max(array_map('count', $array));
$result = [];
for ($i = 0; $i < $maxCount; ++$i) {
    foreach (array_column($array, $i) as $found) {
        $result[] = $found;
    }
}

PHP Snippet 6:

$result = [];
foreach ($array as $i => $row) {
    foreach ($row as $k => $v) {
        $result[$k][$i] = $v;
    }
}
 var_export(array_merge(...$result));