Question

Simple Example:

$a = array(1,2,3,4);
$b = array(10,20,30,40);
$c = array(100,200,300,400);

If I execute the following:

$fp = fopen('somefile.csv','w');
fputcsv($fp,$a);
fputcsv($fp,$b);
fputcsv($fp,$c);
fclose($fp);

I get a file that looks like this:

1,2,3,4
10,20,30,40
100,200,300,400

As I would expect. However what I want is:

1,10,100
2,20,200
3,30,300
4,40,400

Is this possible without just looping and writing each index from each array?

Was it helpful?

Solution

What's wrong with using loops? It is so easy.

$fp = fopen('somefile.csv','w');
for ($i = 0 ; $i < 4 ; $i++){
  fputcsv($fp, array($a[$i], $b[$i], $c[$i])) ;
}
fclose($fp) ;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top