Skip to content

Instantly share code, notes, and snippets.

@cesarmiquel
Created February 13, 2013 03:17
Show Gist options
  • Save cesarmiquel/4941982 to your computer and use it in GitHub Desktop.
Save cesarmiquel/4941982 to your computer and use it in GitHub Desktop.
Read a CSV, process it and output a CSV
<?php
if (count($argv) != 3) {
die('Usage: php remap-csv.php <infile> <outfile>');
}
// in / out files are $argv[1] and $argv[2]
$in = fopen($argv[1], "r");
$out = fopen($argv[2], "w");
$line_num = 1;
while (($data = fgetcsv($in, 1000, ",")) !== FALSE) {
// test data. In this example test for 4 columns
if (count($data) != 4) {
echo "Bad line number [$line_num]\n";
print_r($data);
}
else {
// remap some fields 1 and 3
fputcsv($out, array($data[1], $data[3]), ',', '"');
}
$line_num++;
}
// close files
fclose($in);
fclose($out);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment