<?php
$csv = <<<'CSV'
name, mail, age, bio
john, j@ohn.com, 32,
mary, mary@popp.com, 29,
jeff, brid@ges.co, 47,
"peterson, peter", peter@sson.com, 44, "loves:
hobbies
puzzles"
CSV;
$file = fopen('php://temp', 'rw');
var_dump($file);
echo "\n";
fwrite($file, $csv);
rewind($file);
fpassthru($file);
rewind($file);
echo "\n\n";
function csv($file) {
$header = null;
while ($row = fgetcsv($file)) {
$row = array_map('trim', $row);
if ($header) {
yield array_combine($header, $row);
}
else {
$header = $row;
}
}
}
print_r(iterator_to_array(csv($file)));
preferences:
60.65 ms | 414 KiB | 5 Q