I have a large 2 column csv file (“data.csv”) with a weeks worth of data.
Column one has unix time stamps
Column two has strings:
1329548400,cats
1329548400,dogs
1329550200,fish
1329550200,cats
1329552000,dogs
1329552000,fish
1329584400,cats
1329584400,dogs
1329550200,cats
Via php, I need to convert data.csv into 7 files, Sunday.csv, Monday.csv…Saturday.csv
If I could grab each row, I know I can do something like
$temp = "data.csv";
$fileName = array_unique($temp);
foreach ($fileName as $row) {
$theDay = date("Y", $row[firstcolumn]);
if ($theDay == "Sunday") {
$fileToWriteTo = "Sunday.csv";
$f = fopen($fileToWriteTo, "a");
fwrite($fileToWriteTo, $row . "\n");
fclose($fileToWriteTo);
}
if ($theDay == "Monday") {
$fileToWriteTo = "Monday.csv";
$f = fopen($fileToWriteTo, "a");
fwrite($fileToWriteTo, $row . "\n");
fclose($fileToWriteTo);
}
}
Problem is
1) I don’t know how to read each row of data.csv, assuming the syntax above is wrong
2) I admit Im a bit of a php laymen 🙂
…how far off am I? Am I even in the ballpark?
At first, don’t open file at each iteration do it before loop.
Now, you’ll address fields via
date( 'w'), which will give numbers 0 – Sunday, 6 – Saturday:About making unique results… You’ll probably need to use function like
in_array()and store all processed rows in one array.About reading and writing CSV files, there are 2 functions:
fgetcsv()resp.fputcsv(). So entire loop:And don’t forget to close all files: