...

View Full Version : export array values to csv ??



PHPycho
06-04-2007, 10:57 AM
Hello forums !!
i would like to know how to export the data in .csv format ?
suppose i had an array holding values:

$data_arr[$i][0] = "xx";
$data_arr[$i][0] = "xx";
$data_arr[$i][0] = "xx";
$data_arr[$i][0] = "xx";
where $i varies from 0 to n

I would like to export this data in .csv format,
how could this be accomplished ?
Thanks in advance to all of you

kbluhm
06-04-2007, 03:22 PM
Have a look at the for() or foreach() loops, and the fputcsv() function.

whizard
06-04-2007, 03:52 PM
Also, I believe that fputscsv only works with PHP 5...

if you don't have access to that, something like this should work


//make column headings (if desired)
$contents = "col1,col2,col3,col4,etc\r\n";
$fh = fopen("file.csv","w");
fwrite($fh,
$num = count($data) - 1;
for($i=0;$i<=$num;$i++)
{
$contents .= $data[$i][0].",";
}
$contents .= $data[$num][0]."\r\n";
$fh = fopen("file.csv","w");
fwrite($fh,$contents);
fclose($fh);


HTH
Dan



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum