Reading delimited files… the Simpl way!
fgetcsv()
.Yes, fgetcsv()
is great, and this package uses it under the hood - with some quality of life improvements.
The main benefits of this package are:
null
.composer require simpl/csv
<?php
use Simpl\Csv\Reader;
$csv = Reader::createFromFile('/path/to/your/file.csv');
$csv->setColumns(['name', 'address', 'phone']);
$csv->setSkipRows(1);
while($row = $csv->read())
{
print_r($row['address']);
}
You can use it for any delimited file by calling setDelimiter()
.
<?php
use Simpl\Csv\Reader;
$csv = Reader::createFromFile('/path/to/your/file.csv');
$csv->setColumns(['name', 'address', 'phone']);
$csv->setDelimiter("\t");
$csv->setSkipRows(1);
while($row = $csv->read())
{
print_r($row['address']);
}
Don’t want to get one row at a time? That’s fine. You can get the file as an array or json object.
use Simpl\Csv\Reader;
$csv = Reader::createFromFile('/path/to/your/file.csv');
$csv->setColumns(['name', 'address', 'phone']);
$csv->setSkipRows(1);
$array = $csv->toArray();
$json = $csv->toJson();