tinfanide Posted January 18, 2012 Share Posted January 18, 2012 PHP file $rows = file("php-parse-csv.csv");for ($r=1;$r<count($rows);$r++) {$cells[$r] = explode(",",$rows[$r]);$cols = explode(",",$rows[0]);for ($c=0; $c<3; $c++){ $cells[$r][$cols[$c]] = $cells[$r][$c]; }/* $cells[$r]['name'] = $cells[$r][0];$cells[$r]['######'] = $cells[$r][1];$cells[$r]['age'] = $cells[$r][2];*/ }echo $cells[2]['Age']; // returns error: [b]Notice[/b]: Undefined index: Age in [b]/home/tin/public_html/assessment/php-parse-csv.php[/b] on line [b]58[/b] CSV file Name,######,AgeTin,Male,18Valeriana,Female,17Valerie,Female,16 I could not figure out why it returns an error. php-parse-csv.php Link to comment Share on other sites More sharing options...
JamesB Posted January 18, 2012 Share Posted January 18, 2012 try using the FILE_IGNORE_NEW_LINES flag for file(), i think atm the last token on each line is getting an extra character on the end (\r or \n) Link to comment Share on other sites More sharing options...
tinfanide Posted January 18, 2012 Author Share Posted January 18, 2012 try using the FILE_IGNORE_NEW_LINES flag for file(), i think atm the last token on each line is getting an extra character on the end (\r or \n) Yes, it works as expected. Thank you very much for that. Learnt a new thing. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 18, 2012 Share Posted January 18, 2012 FYI, you can use this also: http://www.php.net/manual/en/function.fgetcsv.php Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.