Reputation: 42
I am trying to parse a csv file using php,
$csv = array_map("str_getcsv", file("upload/" .
$newfilename,FILE_SKIP_EMPTY_LINES));
$keys = array_shift($csv);
foreach ($csv as $i=>$row) {
$csv[$i] = array_combine($keys, $row);
}
foreach ($csv as $row) {
echo $row["Due"];
}
var_dump($csv);
but when I view the dump of the array the Due array key has double quotes
array(20) { [0]=> array(10) { [""Due""]=> string(8) "30/06/17"
The csv file doesn't have 'Due' in quotes, I'm not sure where the extra quotes are coming from, all the rest of the keys are normal
Ive tried the following just to try getting it to work
echo $csv['Due'];
echo $csv['"Due"'];
echo $csv["\"Due\""];
echo $csv["Due"];
but none work
a dump of the keys looks like
array(10) { [0]=> string(8) ""Due"" [1]=> string(5) "Round" [2]=> string(4) "Jobs" [3]=> string(3) "New" [4]=> string(7) "Overdue" [5]=> string(8) "Due Jobs" [6]=> string(9) "Worksheet" [7]=> string(9) "Avg Price" [8]=> string(6) "Weekly" [9]=> string(7) "Monthly" }
and the top line of the CSV file looks like
"Due","Round","Jobs","New","Overdue","Due Jobs","Worksheet","Avg Price","Weekly","Monthly"
Upvotes: 0
Views: 1311
Reputation: 42
I resolved the issue by opening the CSV file and re-doing the quotation marks "
I checked the ASCII values for the quotation marks I removed and replaced them with and they were exactly the same.
I resolved the issue in php by doing a string replace removing any quotation marks "
$keys[0] = str_replace('"', "", $keys[0]);
I am still unsure what was causing this, as when I dumped the $keys variable it didnt show the quotation marks.
Upvotes: 1