https://github.com/bpolaszek/bentools-csviterator
A simple way to iterate over a CSV file.
https://github.com/bpolaszek/bentools-csviterator
Last synced: 11 months ago
JSON representation
A simple way to iterate over a CSV file.
- Host: GitHub
- URL: https://github.com/bpolaszek/bentools-csviterator
- Owner: bpolaszek
- License: mit
- Created: 2014-11-06T09:44:23.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2015-07-21T12:10:52.000Z (over 10 years ago)
- Last Synced: 2025-01-10T09:59:51.879Z (about 1 year ago)
- Language: PHP
- Size: 191 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
CSVIterator
-----------
An easy way to iterate over a CSV file.
Consider the example "example.csv"
"FirstName";"LastName";"City"
"Bill";"Gates";"Seattle"
"Steve";"Jobs";"Palo Alto"
Example usage :
```php
$csv = new \BenTools\CSVIterator\CSVIterator('example.csv', ';');
foreach ($csv as $row)
var_dump($row);
```
Outputs :
array (size=3)
0 => string 'FirstName' (length=9)
1 => string 'LastName' (length=8)
2 => string 'City' (length=4)
array (size=3)
0 => string 'Bill' (length=4)
1 => string 'Gates' (length=5)
2 => string 'Seattle' (length=7)
array (size=3)
0 => string 'Steve' (length=5)
1 => string 'Jobs' (length=4)
2 => string 'Palo Alto' (length=9)
CSVIteratorExtended
-------------------
An extension to CSV Iterator taking the first row as keys.
```php
$csv = new BenTools\CSVIterator\CSVIteratorExtended(new \BenTools\CSVIterator\CSVIterator('example.csv', ';'));
foreach ($csv as $row)
var_dump($row);
```
Outputs :
array (size=3)
'FirstName' => string 'Bill' (length=4)
'LastName' => string 'Gates' (length=5)
'City' => string 'Seattle' (length=7)
array (size=3)
'FirstName' => string 'Steve' (length=5)
'LastName' => string 'Jobs' (length=4)
'City' => string 'Palo Alto' (length=9)
You can optionnally pass a callable as a 2nd argument to ensure you have php-friendly keys :
```php
$csv = new BenTools\CSVIterator\CSVIteratorExtended(new \BenTools\CSVIterator\CSVIterator('example.csv', ';'), 'strtolower');
foreach ($csv as $row)
var_dump($row);
```
Outputs :
array (size=3)
'firstname' => string 'Bill' (length=4)
'lastname' => string 'Gates' (length=5)
'city' => string 'Seattle' (length=7)
array (size=3)
'firstname' => string 'Steve' (length=5)
'lastname' => string 'Jobs' (length=4)
'city' => string 'Palo Alto' (length=9)
Installation
------------
Add the following line into your composer.json :
{
"require": {
"bentools/csviterator": "dev-master"
}
}
Enjoy.