douxu5845
2014-08-06 20:19
浏览 100
已采纳

使用PHP将数据导出到CSV文件 - 输出为空

I thought it will be easy task, but simple I'm not able to output data from an array to CSV file (saved on a server).

Code for that part looks like:

$fp = fopen('missing-skus.csv', 'w');

foreach ($missing_array as $lines) {
    fputcsv($fp, $lines);
}

fclose($fp);

$missing_array looks like:

Array
(
    [0] => 5804
    [1] => 5803
    [2] => 5802
    [3] => 5801
    [4] => 5800
    [5] => 5799
    [6] => 5798
    [7] => 5797
    [8] => 5796
    [9] => 5795
    [10] => 5794
    [11] => 5793
    [12] => 5792
    [13] => 5791
    [14] => 5790
    [15] => 5789
    [16] => 5788
    [17] => 5787
    [18] => 5786
    [19] => 5785
    [20] => 5784
    [21] => 5783
    [22] => 5782
    [23] => 5781
    [24] => 5780
    [25] => 5779
)

No matter what, file is always blank. Any clue what I have missed?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanji1482 2014-08-06 20:31
    已采纳

    The second argument to fputcsv is an array of fields. You are passing a string or integer.

    If you want one line with the array values as fields, in CSV fashion, then just:

    $fp = fopen('missing-skus.csv', 'w');
    fputcsv($fp, $missing_array);
    

    If you just want each array value on one line then no need for fputcsv:

    file_put_contents('missing-skus.csv', implode("
    ", $missing_array));
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题