<?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)));
- Output for 5.6.0 - 5.6.25, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.29, 8.2.0 - 8.2.23, 8.3.0 - 8.3.11
- resource(5) of type (stream)
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"
Array
(
[0] => Array
(
[name] => john
[mail] => j@ohn.com
[age] => 32
[bio] =>
)
[1] => Array
(
[name] => mary
[mail] => mary@popp.com
[age] => 29
[bio] =>
)
[2] => Array
(
[name] => jeff
[mail] => brid@ges.co
[age] => 47
[bio] =>
)
[3] => Array
(
[name] => peterson, peter
[mail] => peter@sson.com
[age] => 44
[bio] => loves:
hobbies
puzzles
)
)
preferences:
61.28 ms | 410 KiB | 5 Q