PHP如何使用fputcsv函数将数组转换为csv [英] PHP How to convert array into csv using fputcsv function

查看:74
本文介绍了PHP如何使用fputcsv函数将数组转换为csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请给我下面的数组:

array(3) {
  [0]=>
  array(2) {
    [0]=>
    string(6) "lkjhgj"
    [1]=>
    string(16) "jhgjhg@jhgjj.com"
  }
  [1]=>
  array(2) {
    [0]=>
    string(5) "hgjk,"
    [1]=>
    string(18) "kjhgfghj@dgdfg.com"
  }
  [2]=>
  array(2) {
    [0]=>
    string(9) "dddd ffff"
    [1]=>
    string(13) "dddd@gmail.fr"
  }
}

我想将其放入一个csv文件中,所以我尝试了:

I want to put it into a csv file, so I've tried :

$fichier = 'file.csv';
$fp = fopen($fichier, 'w');

foreach ($list as $fields) 
{
   fputcsv($fp, $fields);
}

fclose($fp);

header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename='.$fichier);

但是当我下载文件时,发现它是空的!

But when I download the file I found it empty !

请掌握任何想法?预先感谢

Please masters any idea ? Thanks in advance

PS:权限为777

PS : Permissions are 777

推荐答案

 $fichier = 'file.csv';
 header( "Content-Type: text/csv;charset=utf-8" );
 header( "Content-Disposition: attachment;filename=\"$fichier\"" );
 header("Pragma: no-cache");
 header("Expires: 0");

 $fp= fopen('php://output', 'w');

 foreach ($list as $fields) 
 {
    fputcsv($fp, $fields);
 }
 fclose($fp);
 exit();

这篇关于PHP如何使用fputcsv函数将数组转换为csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆