I am trying to remove unnecessary column from my csv. What i do is read from current csv and use fputcsv to produce a new one. However, the data is mess up because fputcsv add extra blank column just before $data[21]
. Below is my code
$file_path = 'test.csv';
$file_output = 'new.csv';
if (file_exists($file_path) && filesize($file_path) > 0) {
if (false !== ($read_file = fopen($file_path, 'r'))) {
$output_file = fopen($file_output, 'w');
while (false !== ($data = fgetcsv($read_file))) {
$outputData = array($data[1], $data[6], $data[19], $data[20],
$data[21]);
fputcsv($output_file, $outputData);
}
}
fclose($read_file);
fclose($output_file);
}
From the different column counts, it appears that the file needs deleted before you run your code. You might want to unset
the file at the start of the execution.
if (file_exists($file_output)) {
unset($file_output);
}
// continue with getting and populating CSV