Jump to content

PHP loop thru array names


tinfanide

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...