export array values to csv ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

export array values to csv ??

Post by PHPycho »

Hello forums !!
i would like to know how to export the data in .csv format ?
suppose i had an array holding values:

Code: Select all

$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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

CSV is comma separated, right? You could join your arrays, then implode() them using a comma. Then, create a CSV file with your result as it's content.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

To make sure that the CSV remains valid i would recommend to wrap all the values in quotes...
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

with some forloops, implode, and a little creativity, this is defenantly feasable.
just be sure to wrap your values in double quotes, and as long as you understant the formatting for .CSV, you should be fine.

I just did this a few weeks ago in the opposite direction... i turned .CSV files into arrays... It's not really as complex as it may seem ;)
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

<?php
# data stored in array
$data_arr[$i][0] = "xx";
$data_arr[$i][0] = "xx";
$data_arr[$i][0] = "xx";
$data_arr[$i][0] = "xx";

# Export Start
header('Content-type: application/x-csv');
header('Content-Disposition: attachment; filename='filename.csv');
header('Content-Description: PHP Generated Data');
for($i = 0; $i < count($data_arr); $i++){
	echo generateCsv($data_arr[$i]);
}
exit(0);
//print_r($data_arr);

function generateCsv($data, $sep = ',', $quot = '"'){ 
	$csv_data = ""; 
	foreach($data as $val){ 		
		$csv_data .= $sep; 
		$csv_data .= $quot.$val.$quot; 
	} 
	return ltrim($csv_data,",")."\r\n"; 
}
?>
its working.....would like to comment on my code
any further improvements ?
Thanks in advance to all of you


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

If your data contains a " then your csv is going to break.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

fputcsv() may be of interest. There are PHP 4 versions available too.
Post Reply