This package is an easy way to read CSV files. The CSV Reader will iterate over a CSV file with low memory usage.
- Returns an array with keys from the headers.
- Reads a specific line in the CSV file.
- Skip the empty lines.
- Reading a CSV file with an offset and/or limit.
Via Composer
$ composer require jeroenzwart/php-csv-iterator
This example is a part of the CSV file ./csv/movies.csv;
name,year_release,order,imdb_rating
Star Wars Episode I – The Phantom Menace,1999,1,6.5
Star Wars Episode II – Attack of the Clones,2002,2,6.5
To loop over each item in the CSV file, you will use the reader like this;
$csv = new \JeroenZwart\CsvIterator\CsvReader('../csv/movies.csv');
$csv->delimiter('"')
foreach ($csv as $line) {
var_dump($line)
// Or do something with $line...
}
// The first dump will look like this
// class stdClass(4) {
// public 'name' => string(42) "Star Wars Episode I – The Phantom Menace"
// public 'year_release' => string(4) "1999"
// public 'order' => string(1) "1"
// public 'imdb_rating' => string(3) "6.5"
// }
Or use one of the default iterator methods;
$csv = new \JeroenZwart\CsvIterator\CsvReader('../csv/movies.csv');
$line = $csv->next()->current();
filePath
(string) - Path to the CSV file.offset
(integer 0) - The offset from start reading the CSV file.limit
(integer -1) - The limit to end reading the CSV file.delimiter
(string ,) - The delimiter character in the CSV file.enclosure
(string ") - The enclosure character in the CSV file.escape
(string \) - The escape character in the CSV file.hasHeaders
(boolean TRUE) - To set if the CSV file has a header, set FALSE if not.asObject
(boolean TRUE) - Return the lines of the CSV file as object, set FALSE as array.keepEmptyLines
(boolean FALSE) - Set TRUE for keeping an empty lines in the CSV file.
Start reading with another position with offset and limit;
$csv = new CsvReader('../csv/actors.csv', 3, 5);
foreach ($csv as $line) {
// Do something with $line...
}
Read with another delimiter, enclose and escape;
$csv = new CsvReader('../csv/actors.csv');
$csv->delimiter(';')->enclose('`')->escape('');
foreach ($csv as $line) {
// Do something with $line...
}
To get the current delimiter, enclosure or escape, you use $csv->delimiter()
Ignore the headers in the CSV file and return array with regular keys, but keep empty lines;
$csv = new CsvReader('../csv/actors.csv');
$csv->headers(false)->asObject(false)->empty(true);
foreach ($csv as $line) {
// Do something with $line...
}
To get the headers of a CSV file as array, you use $csv->headers()
.
For getting the modes for settings asObject or empty, you can use $csv->asObject()
or $csv->empty()
.
Load a line at a given position;
$csv = new CsvReader('../csv/actors.csv');
var_dump($csv->position(3));
To get the current position of the iterator, you use $csv->position()
Please see the changelog for more information on what has changed recently.
Please see the license file for more information.