https://github.com/jwage/easy-csv
EasyCSV is a simple Object Oriented CSV manipulation library for PHP 7.2+
https://github.com/jwage/easy-csv
csv csv-reader php
Last synced: 10 months ago
JSON representation
EasyCSV is a simple Object Oriented CSV manipulation library for PHP 7.2+
- Host: GitHub
- URL: https://github.com/jwage/easy-csv
- Owner: jwage
- License: mit
- Created: 2009-11-13T23:24:05.000Z (about 16 years ago)
- Default Branch: master
- Last Pushed: 2021-04-08T22:20:41.000Z (almost 5 years ago)
- Last Synced: 2025-04-09T17:45:53.392Z (10 months ago)
- Topics: csv, csv-reader, php
- Language: PHP
- Homepage:
- Size: 84 KB
- Stars: 254
- Watchers: 12
- Forks: 58
- Open Issues: 7
-
Metadata Files:
- Readme: README.markdown
- License: LICENSE
Awesome Lists containing this project
README
EasyCSV
=======
EasyCSV is a simple Object Oriented CSV manipulation library for PHP 7.2+
[](http://travis-ci.org/jwage/easy-csv)
[](https://scrutinizer-ci.com/g/jwage/easy-csv/)
[](https://scrutinizer-ci.com/g/jwage/easy-csv/)
[](https://packagist.org/packages/jwage/easy-csv)
[](https://packagist.org/packages/jwage/easy-csv)
## Installation
Install via [composer](https://getcomposer.org/):
```sh
composer require jwage/easy-csv
```
## Reader
To read CSV files we need to instantiate the EasyCSV reader class:
```php
$reader = new \EasyCSV\Reader('read.csv');
```
You can iterate over the rows one at a time:
```php
while ($row = $reader->getRow()) {
print_r($row);
}
```
Or you can get everything all at once:
```php
print_r($reader->getAll());
```
If you have a file with the header in a different line:
```php
// our headers aren't on the first line
$reader = new \EasyCSV\Reader('read.csv', 'r+', false);
// zero-based index, so this is line 4
$reader->setHeaderLine(3);
```
Advance to a different line:
```
$reader->advanceTo(6);
```
More in the Reader unit test.
## Writer
To write CSV files we need to instantiate the EasyCSV writer class:
```php
$writer = new \EasyCSV\Writer('write.csv');
```
You can write a row by passing a commas separated string:
```php
$writer->writeRow('column1, column2, column3');
```
Or you can pass an array:
```php
$writer->writeRow(array('column1', 'column2', 'column3'));
```
You can also write several rows at once:
```php
$writer->writeFromArray(array(
'value1, value2, value3',
array('value1', 'value2', 'value3')
));
```