Transposing an array means turning its rows into columns: the first item of every sub-array becomes the first row, the second items become the second row, and so on. This is handy for CSV-style data where each sub-array is one column (a heading followed by its values) and the columns have different lengths, as in the test code below. A plain nested loop then leaves gaps in the shorter rows: a row can have keys 0 and 2 but no 1, and something like fputcsv(), which ignores keys, would shift the values into the wrong columns. This function fills those gaps with null, so every row of the result has one slot per original sub-array.
function transposeCsvData($data)
{
$ct=0;
foreach($data as $key => $val)
{
//echo count($val);
if($ct< count($val))
$ct=count($val);
}
//echo $ct;
$blank=array_fill(0,$ct,array_fill(0,count($data),null));
//print_r($blank);
$retData = array();
foreach ($data as $row => $columns)
{
foreach ($columns as $row2 => $column2)
{
$retData[$row2][$row] = $column2;
}
}
$final=array();
foreach($retData as $k=>$aval)
{
$final[]=array_replace($blank[$k], $aval);
}
return $final;
}
How it works
- Find the longest sub-array. The first loop counts each sub-array and keeps the largest count in
$ct. In the test below that’s 7, from thebikeslist. - Build an empty grid.
array_fill(0,$ct,array_fill(0,count($data),null))creates$ctrows, each holding onenullper sub-array. For the test data that’s 7 rows of 4nullvalues. - Swap the indexes. The nested loop copies every value from
$data[$row][$row2]to$retData[$row2][$row]. Where a sub-array is shorter, the matching slots are simply never set, so a row like$retData[3]only has keys0and2(patrickandmercedes). - Fill the gaps.
array_replace($blank[$k], $aval)lays the real values over the row ofnullvalues. Every row ends up with all its keys, in order, withnullwherever a value was missing.
Test Code
$a[]=array("name","john","sam","patrick");
$a[]=array("color","orange","green");
$a[]=array("bikes","suzuki","audi","mercedes","bmw","volks","rover");
$a[]=array("techgiants","samsung","amazon");
print_r(transposeCsvData($a));
Output
Array
(
[0] => Array
(
[0] => name
[1] => color
[2] => bikes
[3] => techgiants
)
[1] => Array
(
[0] => john
[1] => orange
[2] => suzuki
[3] => samsung
)
[2] => Array
(
[0] => sam
[1] => green
[2] => audi
[3] => amazon
)
[3] => Array
(
[0] => patrick
[1] =>
[2] => mercedes
[3] =>
)
[4] => Array
(
[0] =>
[1] =>
[2] => bmw
[3] =>
)
[5] => Array
(
[0] =>
[1] =>
[2] => volks
[3] =>
)
[6] => Array
(
[0] =>
[1] =>
[2] => rover
[3] =>
)
)
The empty entries in the output are the null values: print_r() prints null as nothing. Each row of the result is ready to be written out as one line of a CSV file, for example with fputcsv().
Things to watch out for
- The function expects plain lists with keys
0, 1, 2…, like the ones in the test code. If the outer array or the sub-arrays have other keys, they won’t line up with the empty grid and the result will be mixed up. - PHP’s
array_map(null, ...$data)also pads shorter sub-arrays withnulland gives the same result when there are at least two sub-arrays. With only one sub-array, though, it returns that sub-array unchanged instead of transposing it, while the function above still turns it into a column.