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

使用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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘