Separating txt file to many csv files with PHP -
so able parse txt file csv file
$data = array(); while ($line= fgets ($fh)) { $stack = array($laus,$fips,$countyname,$date,$_clf,$_emp,$_unemp,$rate); array_push($data, $stack); } $file = fopen('file.csv','w'); foreach ($data $fields) { fputcsv($file, $fields,',','"'); } fclose($file);
my question is, best way create multiple csv files seperated month (also year, jan01.csv, jan02.csv).
i'm taking bit of guess @ formatting of date
while ($line = fgets ($fh)) { // not sure you're getting these values, i'm assuming it's correct $stack = array($laus,$fips,$countyname,$date,$_clf,$_emp,$_unemp,$rate); // assuming $date looks '2011-10-04 15:00:00' $filename = date('my', strtotime($date)) . '.csv'; $file = fopen($filename,'a+'); fputcsv($file, $stack,',','"'); fclose($file); }
this little slow since you're opening , closing files constantly, since don't know size of original data set don't want use memory caching result before write it.
be aware running multiple times end duplicate data being inserted csv files. may want add code remove/clear out existing csv files before run bit of code.
Comments
Post a Comment