【问题标题】:PHP How to convert array into csv using fputcsv functionPHP如何使用fputcsv函数将数组转换为csv
【发布时间】:2013-02-11 09:22:32
【问题描述】:

请给我以下数组:

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文件中,所以我试过了:

$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);

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

请掌握任何想法?提前致谢

PS : 权限为 777

【问题讨论】:

  • 您的代码示例中的$list 来自哪里?里面有什么?
  • 我没有看到任何代码实际上将文件作为响应的一部分返回!关闭文件后,必须重新打开它才能发送。此外,我在您的代码中没有看到任何错误检查,fopen、fputcsv 和其他文件操作可能会失败。你需要检查他们没有返回 FALSE
  • 使用$fp= fopen('php://output', 'w'); 而不是$fp = fopen($fichier, 'w');.. 看下面我的回答

标签: php csv


【解决方案1】:
 $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();

【讨论】:

  • 我试过这段代码。我收到一个损坏的 csv 文件。无法打开文件。请帮忙。
  • 请在此纯代码答案中提供教育解释。
【解决方案2】:

如果您在会话中保存数组和编码字符的问题。

session_start();
$fp = fopen($_SESSION['CSV']['type'].'.csv', 'w');
foreach ($_SESSION['CSV'] as $fields) {
    $val1 = htmlspecialchars_decode(utf8_decode(html_entity_decode($fields[0], ENT_QUOTES | ENT_XML1, 'UTF-8')));
    $val2 = htmlspecialchars_decode(utf8_decode(html_entity_decode($fields[1], ENT_QUOTES | ENT_XML1, 'UTF-8')));
    $val3 = htmlspecialchars_decode(utf8_decode(html_entity_decode($fields[2], ENT_QUOTES | ENT_XML1, 'UTF-8')));
    fputcsv($fp, array($val1,$val2,$val3,$val4,$val5));
}
fclose($fp);

【讨论】:

    【解决方案3】:

    如果有人正在寻找将带有动态标题的查询的结果导出到数组然后导出到 csv 文件,您可以使用以下函数:

    function exportCSVFile($fieldsNames, $result)
    {
    
    $fileName = "result.csv";
    header("Content-type: text/csv");
    header("Content-Disposition: attachment; filename=$fileName");
    header("Pragma: no-cache");
    header("Expires: 0");
    $stdout = fopen('php://output', 'w');
    
    fputcsv($stdout, $fieldsNames, ',', '"'); // put the headers or fields Names
    
    $resultArray = array();
    
    $tempRowHolder = array();
    
    /* 
        build the finalized array which will be used to export the result as csv 
    */
    
    for ($i = 0; $i < count($result); $i++) {
        for ($j = 0; $j < count($fieldsNames); $j++) {
            $tempRowHolder[$j] = $result[$i][$fieldsNames[$j]]; // fetch a row by the different fields names
        }
    
        /* push the row from the tempRowHolder array to the main array($resultArray) */
        array_push($resultArray, $tempRowHolder);
    
        /* clear the temporary array (the holder of the row) to use it fresh in the next iteration */
        $tempRowHolder = [];
    }
    
    $i = 0;
    
    /* put the finalized array into the csv file  */
    while ($i < count($resultArray)) {
        fputcsv($stdout, $resultArray[$i++]);
    }
    
    fclose($stdout);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 2018-01-04
      • 2013-05-12
      • 1970-01-01
      • 2017-08-14
      • 2016-03-18
      • 1970-01-01
      相关资源
      最近更新 更多