PHP fputcsv()函数
The fputcsv()函数可以将一行格式化为CSV,并将其写入打开的文件中。此函数可以返回写入字符串的长度,或者在失败时返回false。
语法
int fputcsv ( resource handle , arrayfields [, string delimiter = "," [, stringenclosure = '"' [, string $escape_char = "\\" ]]] )
示例
<?php
list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"') );fp = fopen("/PhpProject/sample.csv", "w");
foreach(list asfields) {
fputcsv(fp,fields);
}
echo "The list data has been inserted successfully to .csv file";
fclose($fp);
?>
输出
The list data has been inserted successfully to .csv file