I'm using the following call:
$csv = array_map('str_getcsv', file($file), array($delimiter, '"', "\\"));
The first line is parsed into an array properly, but the second line remains as a string. The CSV delimiter is consistent in the test file (semicolon), and there are no strange characters that I can find. The file opens normally in Excel if I add sep=;
. What issues could be causing this?
This is a misunderstanding of how array_map works (the language in the docs is rather confusing).
Taking the given snippet:
$csv = array_map('str_getcsv', file($file), array($delimiter, '"', "\\"));
This is functionally similar to doing the following:
str_getcsv('line one of $file', $delimiter);
str_getcsv('line two of $file', '"');
str_getcsv('line three of $file', "\\");
This is why the first line appears to work, as the delimiter is correct, but the subsequent ones do not as the second param is incorrect in context of the CSV.