Last active
December 11, 2015 11:08
-
-
Save nyamsprod/a0807df54a29d1a89e48 to your computer and use it in GitHub Desktop.
Examples for the release of League\Csv 8.0.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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