问题
This is what my csv file looks like:
I have three fields (import site, import goods, import status) to import respective data. So if a user clicks on import site only sites name will get saved and the rest won't, similarly for import goods and import status.
The site's data and status's data are saved in a single table but the goods data is being saved in other table with respect to it's site. How do i save them into multiple tables?
回答1:
I will share simple snippet to parse csv files, maybe this can help
$i=0; $keys=array();$output=array();
$handle=fopen($filename, "r");
if ($handle){
while(($line = fgetcsv($handle)) !== false) {
$i++;
if ($i==1) {
$keys=$line;
}
elseif ($i>1){
$attr=array();
foreach($line as $k=>$v){
$attr[$keys[$k]]=$v;
}
$output[]=$attr;
}
}
fclose($handle);
}
This will do the job, i'm using it always when come to csv. To make this work 1st line must contain keys, for example:
import_site, import_goods, import_status
In your $output array you'll have data with keys $output["import_site"] and so on.
Hope this will be helpfull.
回答2:
In case problems with csv generated on mac remember do this:
ini_set("auto_detect_line_endings", true);
can save a lots of time ;)
来源:https://stackoverflow.com/questions/21155461/importing-and-saving-data-from-csv-file-in-yii