How to Create Comma Separated Data in PHP
Posted by admin | Posted in PHP, Software Programs, Theory Subjects | Posted on 23-11-2009
0
Use the fputcsv( ) function to generate a CSV-formatted line from an array of data. Writes the data in $sales into a file
Sample Program
<?php
$sales = array( array(‘Northeast’,’2005-01-01′,’2005-02-01′,12.54),
array(‘Northwest’,’2005-01-01′,’2005-02-01′,546.33),
array(‘Southeast’,’2005-01-01′,’2005-02-01′,93.26),
array(‘Southwest’,’2005-01-01′,’2005-02-01′,945.21),
array(‘All Regions’,'–’,'–’,1597.34) );
ob_start();
$fh = fopen(‘php://output’,'w’) or die(“Can’t open php://output”);
foreach ($sales as $sales_line) {
if (fputcsv($fh, $sales_line) === false) {
die(“Can’t write CSV line”);
}
}
fclose($fh) or die(“Can’t close php://output”);
$output = ob_get_contents();
ob_end_clean();
?>
<?php
$sales = array( array(‘Northeast’,’2005-01-01′,’2005-02-01′,12.54),
array(‘Northwest’,’2005-01-01′,’2005-02-01′,546.33),
array(‘Southeast’,’2005-01-01′,’2005-02-01′,93.26),
array(‘Southwest’,’2005-01-01′,’2005-02-01′,945.21),
array(‘All Regions’,'–’,'–’,1597.34) );$fh = fopen(‘sales.csv’,'w’) or die(“Can’t open sales.csv”);
foreach ($sales as $sales_line) {
if (fputcsv($fh, $sales_line) === false) {
die(“Can’t write CSV line”);
}
}
fclose($fh) or die(“Can’t close sales.csv”);
?>
Putting comma-separated data into a string
Printing comma-separated data
<?php $sales = array( array('Northeast','2005-01-01','2005-02-01',12.54),array('Northwest','2005-01-01','2005-02-01',546.33),array('Southeast','2005-01-01','2005-02-01',93.26),array('Southwest','2005-01-01','2005-02-01',945.21),array('All Regions','--','--',1597.34) );$fh = fopen('php://output','w');foreach ($sales as $sales_line) {if (fputcsv($fh, $sales_line) === false) {die("Can't write CSV line");}}fclose($fh);?>
