fgetcsv输出不出现

问题描述:

我正在制作一次性,单一用途的CSV格式转换脚本。我从一个提供者处获取文件,并将其字段映射到另一个提供者的格式。fgetcsv输出不出现

到目前为止,我有我想通过一个PHP脚本,将 1.去掉标题行 2.将CSV数据到一个数组 3.打印第一的内容运行CSV文件数组中的字段(这样我就可以看到它的工作)

CSV文件看起来是这样的例子:

Order Date (UTC Time Zone),Order Number,Customer Name,Customer Address,Customer Address2,City,State,Zip,Country,Product Name,Quantity,UPC,SKU,Shipping Carrier,Tracking Number 
2017-10-09,1000-000001-000001,John Doe,1234 ANY DR,"",ANY CITY,CA,90210,US,ACME® Product,1,,,"","" 

的问题是,我的脚本,下面,打印出什么。我只想验证我有一个数组,并且它所包含的数据正在登录到正确的数组索引中。

<?php 
// Remove headers from the first line of file "yyyymmdd.csv" and save it to an outfile named "yyyymmdd_clean.csv" 
ini_set('auto_detect_line_endings', true); 
$original = fopen("20171010.csv", "r"); 
$first = fgets($original,2048); #get first line. 
$outfile="20171010_clean.csv"; 
$o = fopen($outfile,"w"); 
while (!feof($original)) { 
    $buffer = fgets($original,2048); 
    // Save outfile 
    fwrite($o,$buffer); 
} 
// Close original file, but don't close outfile 
fclose($original); 

// Convert cleaned outfile into array of SourceLine[n],SourceField[n] 
while (($data = fgetcsv($o)) !== FALSE) { 
    echo "OrderID " . $data[0]; 
} 
// Now close the outfile. We're done. 
fclose($o); 
?> 

我在远程开发服务器上运行它。

我需要更改哪些内容才能将数据的第一个字段回显到屏幕?

+1

您需要[rewind()](http://www.php.net/manual/en/function.rewind.php)输出文件指针,因为它位于文件末尾 –

+1

您也是以“只写”模式打开,而不是“读/写”模式;你需要''w +'',而不是简单''w“' –

+0

谢谢。那是我错过的。 –

由于Mark Baker声明,问题是我没有使用rewind(),我没有打开它在读/写模式(w+)。