Skip to content

Core\Csv::rows

Reads CSV data one record at a time, so the whole file never has to be in memory.

Core\Csv::rows reads from an open file, an uploaded file, Core\Request::bodyStream(), bytes, or a list or loop that gives bytes pieces. Pass the file or the upload directly. Do not read it into a variable first. You read the result with a foreach loop, and each step gives you one record as an array of text fields. Only one record is kept at a time.

A file is read from its first byte. You can loop over the result only once. Set header to true and the first record gives the column names. Every record after it uses those names as keys.

The separator, quote and escape options are the same as for Core\Csv::parse. Set charset for a file that is not UTF-8, such as Core\Charset::Windows1252. A byte order mark at the start sets the charset for you.

Good to know: Core\Csv::rows does not open or close a file. Close it yourself when the loop is done.

Parameters

  • $sourceCore\Bytes::Source|Core\Bytes::ChunksThe bytes to read. An open file is read from its first byte, whatever its current position is. A loop over bytes pieces is read once, from the first piece to the last.

Options

  • separatorstringoptionalThe one ASCII character between fields. The default is ,.
  • quotestringoptionalThe one ASCII character that quotes a field. The default is ".
  • escapestringoptionalThe one ASCII character that escapes a quote inside a quoted field. The default is the empty string, which means no escape character. Then two quotes are read as one quote, as RFC 4180 says.
  • headerbooloptionalWhen true, the first record is read as column names, and every record uses them as keys. By default, each field has its column number as the key.
  • charsetCore\CharsetoptionalThe charset of the bytes, such as Core\Charset::Windows1252. The default is Core\Charset::Utf8. The bytes are converted to text one piece at a time, so the whole file is never in memory. When the bytes start with a byte order mark for UTF-8 or UTF-16, that mark sets the charset, and it is not part of the first field.

Return value

Returns Core\Csv\Rows<string>. The records, one at a time. Each record is an array of string fields with the same keys that parse gives. The fields are tainted string when the bytes come from the request. Nothing is read until the loop starts.

Errors

  • RuntimeErrorAn option for the CSV format is empty, longer than one ASCII character, or CR or LF. It is also thrown when two of separator, quote and escape are the same character, or the file was closed before or during the loop.
  • IOErrorThe operating system could not read the file during the loop.
  • ParseErrorA field is not valid UTF-8. The message gives the line where the field ends, and the loop stops there. With another charset, the bytes are not text in that charset. Then the message gives the position of the first wrong byte.

Examples

Read a file one record at a time

<?nvs
// Shows how to read a CSV file one record at a time. `Core\Csv::rows` holds a
// single record, so a file of any size can be read this way.
// The program writes a small file first, so that it has one to read.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'orders.csv');
Core\IO::write($path, "fig,2\napple,10\n");
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);
// This prints `fig 2` and `apple 10`.
foreach (Core\Csv::rows($file) as array<string> $record) {
echo $record[0], ' ', $record[1], "\n";
}
$file->close();
Output
fig 2
apple 10

Name the columns from the first record

<?nvs
// Shows how to read a file whose first line names the columns. With
// `{ header: true }` every record is keyed by those names.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'stock.csv');
Core\IO::write($path, "product,count\nfig,2\napple,10\n");
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);
// The first record holds the column names and is not one of the records you
// read. This prints `fig: 2` and `apple: 10`.
foreach (Core\Csv::rows($file, { header: true }) as array<string> $record) {
echo $record['product'], ': ', $record['count'], "\n";
}
$file->close();
Output
fig: 2
apple: 10

Add up a column of a large export

<?nvs
// Shows how to add up one column of a sales export. The file is read record by
// record, so a file of ten gigabytes needs as much memory as this one.
string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'sales.csv');
Core\IO::write(
$path,
"date;region;amount\n"
. "2026-01-02;north;120.50\n"
. "2026-01-02;south;99.00\n"
. "2026-01-03;north;18.25\n",
);
Core\IO\File $file = Core\IO::open($path, Core\IO::FileMode::Read);
float $north = 0.0;
int $records = 0;
// The spreadsheet that wrote this file separates fields with a semicolon.
foreach (Core\Csv::rows($file, { separator: ';', header: true }) as array<string> $sale) {
$records = $records + 1;
if ($sale['region'] == 'north') {
// A field is always text. `as ?float` converts it to a number, and
// gives `null` for a field that is not one.
$north = $north + (($sale['amount'] as ?float) ?? 0.0);
}
}
$file->close();
// This prints `3 records, north 138.75`.
echo $records, ' records, north ', $north, "\n";
Output
3 records, north 138.75