我想将数组数据导出到csv文件中,我创建了一个文件tset.csv并尝试使用以下代码将数据写入该文件中
这是我的数组:
Array (
[0] => Array ( [pgm_name] => General services )
[1] => Array ( [pgm_name] => Department of education )
)
代码:
$filename = 'test.csv';
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/csv; ");
// header("Pragma: no-cache");
// header("Expires: 0");
$file = fopen(FCPATH.'/uploads/'.$filename, 'w');
fprintf($file, chr(0xEF).chr(0xBB).chr(0xBF));
$header = array("Program Name");
// fputcsv($file, $header);
foreach($printdata as $printdata) {
fputcsv($file,$printdata);
}
fclose($file);
exit;
在csv文件中我遇到这样的错误
遇到PHP错误严重性:警告
消息:fputcsv()期望参数2是给定的数组字符串
文件名:controllers / Frontdoor.php
行号:3516
“
回溯:
”
答案 0 :(得分:0)
请选中“ foreach($ printdata as $ printdata){”。
您应该使用
counter=key;
counter -=1
for c,i in enumerate(upper_half):
if i.startswith('<') and not i.startswith('</'):
if (i.split('>')[0].split('<')[1]):
upper_tags.append(i.split('>')[0].split('<')[1])
upper_tag_index.append(c)
counter=counter -1;
upper_dict=dict(zip(upper_tag_index,upper_tags))
upper_dict_temp=upper_dict
for index,value in enumerate(upper_half):
for (k,val) in upper_dict_temp.items():
if '</'+val+'>' in value:
upper_dict.pop(k)
break;
print("Tags above key",upper_dict)
答案 1 :(得分:0)
使用此代码。希望它对您有帮助
$output = fopen("C:/xampp/htdocs/data.csv",'w') or die("Can't open C:/xampp/htdocs/data.csv");
header("Content-Type:application/csv");
header("Content-Disposition:attachment;filename=$filename");
fputcsv($output, array('pgm_name'));
foreach($printdata as $value) {
fputcsv($output, $value);
}
fclose($output) or die("Can't close C:/xampp/htdocs/data.csv");