How to store each row of the csv file to array in php -


i have data in csv file follows.

mr,vinay,h s,application engineer,vinay.hs@springpeople.com,99005809800,yes mr,mahammad,hussain,application engineer,vinay.hs@springpeople.com,99005809800,yes

i want store each row in each array.explain me how

assuming csv file (if not, make csv match $csv, file broken down in separate lines), here simple way:

$csv = file('mycsv.csv'); $result = array(); //iterate lines foreach ($csv $line)   // i'd recommend  more robust "csv-following" method though (fgetcsv maybe)   $result[] = explode(',',$result);  //show results print_r($result); 

though fgetcsv safer bet (as others have mentioned) (see docs).


to further extend comment:

//iterate lines foreach ($csv $line)   // i'd recommend  more robust "csv-following" method though (fgetcsv maybe)   $result[] = explode(',',$result);  

can become:

//iterate lines foreach ($csv $line)   // combine current results new results   $result = array_marge($result,$line); 

or, if order important:

//iterate lines foreach ($csv $line)   // add each result on end of result array   foreach($line $val)     // add each value     $result[] = $val; 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -