Skip to content

Instantly share code, notes, and snippets.

@nyamsprod
Last active December 11, 2015 11:08
Show Gist options
  • Select an option

  • Save nyamsprod/a0807df54a29d1a89e48 to your computer and use it in GitHub Desktop.

Select an option

Save nyamsprod/a0807df54a29d1a89e48 to your computer and use it in GitHub Desktop.
Examples for the release of League\Csv 8.0.0
<?php
use League\Csv\Reader;
$formatter = function (array $row) {
$row['create_at'] = new DatetimeImmutable($row['create_at']);
return $row;
};
$reader = Reader::createFromPath('/path/to/file.csv');
$iterator = $reader->fetchAssoc(0, $formatter); //$iterator is a Iterator
foreach ($iterator as $data) {
echo $row['create_at']->format('Y-m-d H:i:s'); //because this is a DateTimeImmutable object
}
<?php
use League\Csv\Reader;
$reader = Reader::createFromPath('/path/to/file.csv');
$iterator = $reader->fetchPairs(); //$iterator is a Generator
foreach ($iterator as $firstname => $lastname) {
echo $firstname, ' -> ', $lastname, PHP_EOL;
//display 'john -> doe'
}
$reader = Reader::createFromPath('/path/to/file.csv');
$arr = $reader->fetchPairsWithoutDuplicates(); //$arr is an array
foreach ($arr as $firstname => $lastname) {
echo $firstname, ' -> ', $lastname, PHP_EOL;
//display 'john -> doe'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment