[英]PHP probleme while remove columns in csv
I use the php script below to remove some columns from a csv, order them new and save it as new file. 我使用下面的php脚本从csv中删除了一些列,对其进行了新的排序并将其另存为新文件。 And it works for the file i made it for.
它适用于我制作的文件。 Now i need to do the same with another csv but i don't know whats wrong.
现在我需要对另一个csv执行相同的操作,但是我不知道怎么了。 I always get a comma befor the data in the first column.
在第一列中的数据之前,我总是得到一个逗号。
This is what i have, but it doesn't really work. 这就是我所拥有的,但实际上并没有用。
<?php
$input = 'http://***/original.csv';
$output = 'new.csv';
if (false !== ($ih = fopen($input, 'r'))) {
$oh = fopen($output, 'w');
while (false !== ($data = fgetcsv($ih))) {
// this is where you build your new row
$outputData = array($data[4], $data[0]);
fputcsv($oh, $outputData);
}
fclose($ih);
fclose($oh);
}
The original.csv looks that: original.csv看起来:
subproduct_number barcode stock week_number qty_estimate productid variantid
05096470000 4024144513543 J 3 6 35016
ae214 848518017215 N 23 0 7 35015
05097280000 4024144513727 J 1 32 34990
The seperator is ';'. 分隔符为“;”。 The same seperator is used in the file that is working
正在使用的文件中使用相同的分隔符
But here it will be go wrong because my saved new.csv looks like this: 但是这里会出错,因为我保存的new.csv如下所示:
subproduct_number barcode stock week_number qty_estimate productid variantid
,05096470000 4024144513543 J 3 6 35016
,ae214 848518017215 N 23 0 7 35015
,05097280000 4024144513727 J 1 32 34990
But what i need is a new csv that looks like this: 但是我需要的是一个新的csv,看起来像这样:
qty_estimate subproduct_number
3 05096470000
0 ae214
1 05097280000
As you can see, i need only the 5. column ($data[4]) as first and the first column ($data[0]) as the second one. 如您所见,我只需要5.列($ data [4])作为第一列,而第一列($ data [0])作为第二列。
I hope someone can point me in the reight direction. 我希望有人能指出我正确的方向。 Thanks
谢谢
You can do so: 您可以这样做:
while (false !== ($data = fgetcsv($ih))) {
$data = explode(';', $data[0]);
$outputData = array($data[4], $data[0]);
fputcsv($oh, $outputData, ';');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.