I’m having a hard time getting the CSV helper code to work with multi-line data in the CSV. One of my fields has many paragraphs and therefore line breaks.
The specific error is array_combine(): Both parameters should have an equal number of elements
, and when I remove the line breaks, it works again. But I have 1800 projects and can’t feasibly continually add and remove line breaks.
Should I be using fgetcsv() instead?
Edit:
I found a Kirby question on Stack Overflow and this code worked as a custom siteMethod…
'csvToArray' => function($file, string $delimiter = ',', int $length = 8000): array {
$fh = fopen($file, $length, $delimiter);
$header_line = fgets($fh);
$header_line = str_replace("\xEF\xBB\xBF", '', $header_line);
$keys = str_getcsv($header_line);
$csv = [];
while ($row = fgetcsv($fh, $delimiter)) {
$csv[] = array_combine($keys, $row);
}
return $csv;
}