由于我最近通过PHP使用CSV,所以我在询问有关通过PHP取消CSV行的问题之后又提出了另一个问题:Unset rows (based on row name) csv through php
我在第一行名称上有一个CSV文件:col1; col2; col3,col4,col5
如何更改特定列的名称,例如:" col1"到"第1列和第34列,"第4列"到第4栏和第34栏。
这就是我现在所拥有的:
<?php
$inFile = 'removed_headers.csv';
$outFile = 'final_headers.csv';
$delimiter = ';';
$enclosure = '"';
$originals = array(0 => "RBI_Articlenr.", 1 => "Brand");
$replacements = array(0 => "sku", 1 => "manufacturer");
$write = fopen($outFile, 'w');
if ($write) {
$rows = file($inFile);
$first = false;
foreach($rows as $row) {
$csvToPHP = str_getcsv($row, $delimiter, $enclosure);
if (!$first) {
$headers = array_flip($csvToPHP);
$first = true;
}
$headers = array_replace($originals, $replacements);
// now the headers array is how it should be
// how to set the value's of $header to $csvToPHP ?
fputcsv($write, $csvToPHP, $delimiter, $enclosure);
}
}
fclose($write);