doudao1922 2016-01-27 19:33
浏览 85
已采纳

在PHP中,fgetcsv将文件转换为一个数组

I have a csv file I'm uploading that is structured like this:

SKU,STATUS,SUPPLIER,PRODUCT TYPE,PRODUCT SUBTYPE,PRODUCT STYLE,PRODUCER NAME,PRODUCT NAME,VINTAGE,RAW MATERIALS
00788305,Active,Pabst Brewery,Beer,Specialty,Spice/Herb/Vegetable,Small Town Brewery,Not Your Father's Root Beer (Units of 4),,
00380314,Active,The Patr¢n Spirits Company,Spirit,Tequila,Extra A§ejo,Patr¢n,En Lalique Serie 1 Extra A§ejo Tequila,,
00255814,Active,Huneeus Vintners,Wine,Still,White,Veramonte,Valle de Casablanca Chardonnay,,Chardonnay

The csv was created from an excel spreadsheet export.

When I run fgetcsv against the uploaded file I get one long array instead of each line placed into its own array as expected.

  • 写回答

1条回答 默认 最新

  • dtrj74376 2016-01-27 20:06
    关注

    From the PHP Docs:

    Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.

    fgetcsv() will only read one row at a time, so you will need to loop over the file calling fgetcsv() to read each row in turn until you reach the end of the file.

    while (($data = fgetcsv($fh, 1024, ",")) !== FALSE) {
        // Do whatever you need to do with the data
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!