note 22273 modified in function.fgetcsv by aidan
| From: | aidan@php.net | Date: | Thu, 01 Jul 2004 01:05:23 +0000 |
| Subject: | note 22273 modified in function.fgetcsv by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72216@lists.php.net to get a copy of this message | ||
function to parse multi arrays into csv data
array in... array of array(datasets); first dataset = field names.
usage:
$toparse[0][0] = "field1";
$toparse[0][1] = "field2";
$toparse[1][0] = "value1";
$toparse[1][1] = "123123123"; // to see
echo export_to_csv($toparse);
<?php
function export_to_csv($inarray){
while (list ($key1, $val1) = each ($inarray)) {
while (list ($key, $val) = each ($val1)) {
if (is_numeric($val)){
$sendback .= $val.",";
}else{
$sendback .= "\"". $val ."\",";
}//fi
}//wend
$sendback = substr($sendback, 0, -1); //chop last ,
$sendback .= "\n";
}//wend
return ($sendback);
}// end function
?>
send the file to the client.. pretty simple.
usage: send_file_to_client("data.csv",export_to_csv($data));
<?php
function send_file_to_client($filename, $data){
header("Content-type: application/ofx");
header("Content-Disposition: attachment; filename=$filename");
echo $data;
};
?>
--was--
// function to parse multi arrays into csv data
// array in... array of array(datasets); first dataset = field names.
// usage:
/*
$toparse[0][0] = "field1";
$toparse[0][1] = "field2";
$toparse[1][0] = "value1";
$toparse[1][1] = "123123123"; // to see
echo export_to_csv($toparse);
*/
function export_to_csv($inarray){
while (list ($key1, $val1) = each ($inarray)) {
while (list ($key, $val) = each ($val1)) {
if (is_numeric($val)){
$sendback .= $val.",";
}else{
$sendback .= "\"". $val ."\",";
}//fi
}//wend
$sendback = substr($sendback, 0, -1); //chop last ,
$sendback .= "\n";
}//wend
return ($sendback);
}// end function
// send the file to the client.. pretty simple.
//usage:send_file_to_client("data.csv",export_to_csv($data));
function send_file_to_client($filename, $data){
header("Content-type: application/ofx");
header("Content-Disposition: attachment; filename=$filename");
echo $data;
};
http://php.net/manual/en/function.fgetcsv.php