Core\Csv::parse
Core\Csv::parse(string $text, {…}): array<array<string>>Core\Csv::parse( string $text, {…}, ): array<array<string>>
Reads a CSV document and gives you its records, each one an array of text fields.
Core\Csv::parse takes the whole document as a string. Each record becomes one array, holding its fields in the order they are written. A field in quotes may contain the separator, a line break, or a quote written twice, and you get that text back without the quotes around it. A record ends with a newline, or with a carriage return and a newline.
Set header to true and the first record becomes the column names. Every later record is then keyed by those names, and the header itself is not part of the result. The separator, quote and escape options read a document written with other characters.
Good to know: a record with fewer fields than the header keeps only the fields it has, and nothing is filled in for the rest. A record with more fields keys the extra ones by their position.
Parameters
$textstringThe whole CSV document.
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.headerbooloptionalWhentrue, the first record is read as column names, and every returned row uses them as keys. By default, each field has its column number as the key.
Return value
Returns array<array<string>>. One array per record, and every field is a string. The keys are the column numbers. With header, the keys are the names from the header. A field after the last header name keeps its column number. The header row is not in the result. The document never fails to parse, and an empty document gives an empty array.
Errors
RuntimeErrorAn option for the CSV format is empty, longer than one ASCII character, orCRorLF. It is also thrown when two ofseparator,quoteandescapeare the same character.
Examples
Read a small document
<?nvs// Shows how to read a CSV document. `Core\Csv::parse` gives you one array per// record, holding the fields in the order they are written.
string $document = "fig,2,3.50\napple,10,0.40\n";
var $records = Core\Csv::parse($document);
// This prints `fig sold 2` and `apple sold 10`.foreach ($records as array<string> $record) { echo $record[0], ' sold ', $record[1], "\n";}fig sold 2apple sold 10Use the first record as column names
<?nvs// Shows how to read a document whose first record holds the column names. With// `{ header: true }` every later record is keyed by those names, and the first// record is not part of the result.
string $document = "name,qty,price\nfig,2,3.50\napple,10,0.40\n";
var $records = Core\Csv::parse($document, { header: true });
// This prints `fig costs 3.50` and `apple costs 0.40`.foreach ($records as array<string> $record) { echo $record['name'], ' costs ', $record['price'], "\n";}fig costs 3.50apple costs 0.40Add up a column of an upload
<?nvs// Shows how to read a price list somebody uploaded and add up one column. The// file was written by a spreadsheet, so the fields are separated by semicolons// and one product name contains a semicolon of its own.
string $upload = "product;count;price\n" . "Beans, dried;3;4.10\n" . "\"Salt; coarse\";2;1.50\n";
var $lines = Core\Csv::parse($upload, { separator: ';', header: true });
float $total = 0.0;foreach ($lines as array<string> $line) { // A field is always text, so the two numbers are converted before they are // used. `as ?float` gives `null` for a field that is not a number. float $price = ($line['price'] as ?float) ?? 0.0; float $count = ($line['count'] as ?float) ?? 0.0; $total = $total + $price * $count;}
// This prints `order total: 15.3`.echo 'order total: ', $total, "\n";order total: 15.3