lantongxue/csv-reader

A high-performance and low-memory CSV reader

dev-main 2025-03-13 14:30 UTC

This package is auto-updated.

Last update: 2025-04-13 14:43:04 UTC


README

A high-performance and low-memory CSV reader

Usage

install

composer require lantongxue/csv-reader

CSVReader constructor parameters:

CSVReader(string $csvFile, bool $firstRowIsHeader = true, array $getcsvParams = [])

$csvFile Specify CSV file path

$firstRowIsHeader Setting the first row as the header row, default true

$getcsvParams This parameter exists for compatibility with php8, but also improves the flexibility of csv reading, the specific parameter structure is as follows:

[
    'separator' => ",",
    'enclosure' => "\"",
    'escape' => "\\",
];

Refer to the fgetcsv function for the exact meaning.

Reading through an iterator

$csv = 'test.csv';
$reader = new lantongxue\CSVReader($csv);
foreach($reader as $row) {
    // todo
}

Chunked read

$csv = 'test.csv';
$reader = new lantongxue\CSVReader($csv);
$reader->Chunk(function($rows) {
    foreach($rows as $row) {
        // todo
    }
    return true;
}, 1000);

Other Api

GetHeader() Return header row when the $firstRowIsHeader is true

License

MIT