https://github.com/stilliard/csvparser
Quickly take in and output csv formats, also map functions to columns and rows and many more simple csv operations.
https://github.com/stilliard/csvparser
csv-format php
Last synced: 5 months ago
JSON representation
Quickly take in and output csv formats, also map functions to columns and rows and many more simple csv operations.
- Host: GitHub
- URL: https://github.com/stilliard/csvparser
- Owner: stilliard
- License: mit
- Created: 2014-03-14T11:40:37.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2025-08-13T09:12:27.000Z (7 months ago)
- Last Synced: 2025-10-13T16:07:52.879Z (5 months ago)
- Topics: csv-format, php
- Language: PHP
- Size: 69.3 KB
- Stars: 11
- Watchers: 3
- Forks: 5
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Csv Parser
Quickly take in and output csv formats.
[](https://travis-ci.org/stilliard/CsvParser)
[](https://scrutinizer-ci.com/g/stilliard/CsvParser/)
[](https://scrutinizer-ci.com/g/stilliard/CsvParser/)
[](https://packagist.org/packages/stilliard/csvparser) [](https://packagist.org/packages/stilliard/csvparser) [](https://packagist.org/packages/stilliard/csvparser) [](https://packagist.org/packages/stilliard/csvparser)
[](https://app.fossa.io/projects/git%2Bgithub.com%2Fstilliard%2FCsvParser?ref=badge_shield)
## Install
```bash
composer require stilliard/csvparser 1.3.2
```
## Example usage:
```php
use CsvParser\Parser;
//
// Simple array to string usage
//
$array = [['id'=>1, 'name'=>'Bob'],['id'=>2, 'name'=>'Bill']];
$parser = new Parser();
$csv = $parser->fromArray($array);
var_dump($parser->toString($csv));
```
## Example stream reading (better memory optimisations)
```php
// stream reading from a CSV file
foreach (Parser::stream(__DIR__ . '/your/path/input.csv') as $row) {
var_dump($row);
}
// write file
Parser::write($data, __DIR__ . '/your/path/output.csv');
```
```php
//
// Full power examples:
//
// setup initial parser
$parser = new \CsvParser\Parser(',', '"', "\n");
// change settings after init
// set column delimiter
$parser->fieldDelimiter = ';';
// set text enclosure
$parser->fieldEnclosure = "'";
// set line delimiter
$parser->lineDelimiter = "\n";
// Input (returns instance of \CsvParser\Csv)
$csv = $parser->fromArray([['id'=>1, 'name'=>'Bob'],['id'=>2, 'name'=>'Bill']]);
$csv = $parser->fromString("id,name\n1,Bob\n2,Bill");
$csv = $parser->fromFile('demo.csv');
// get row count
var_dump($csv->getRowCount());
// get the first row as array from the csv
var_dump($csv->first());
// get the column headings / keys
var_dump($csv->getKeys());
// want to force a column sort / index?
$csv->reKey(['id', 'name', 'email']);
// append/prepend rows
$csv->appendRow(['id'=>3, 'name'=>'Ben']);
$csv->prependRow(['id'=>4, 'name'=>'Barry']);
// map function over column
$csv->mapColumn('name', 'trim');
$csv->mapColumn('name', function ($name) {
return trim($name);
});
// map function over rows
$csv->mapRows(function ($row) {
$row['codename'] = base64_encode($row['id']);
return $row;
});
// add a column
$csv->addColumn('codename', 'default value');
// remove a column
$csv->removeColumn('codename');
// filter down rows
$csv->filterRows(function ($row) {
return $row['id'] != '#'; // remove rows where the id column just has a hash inside
});
// remove row by index
$csv->removeRowByIndex(4);
// or remove row(s) by column value, such as id 22
$csv->removeRow('id', 22);
// or remove row(s) by multiple creiteria, such as when id 22 AND when name is 'some name'
$csv->removeRows(['id'=>22, 'name'=>'some name']);
// Column reordering
$csv->reorderColumn('colname', 0); // move to position 0 (the start)
// or multiple
$csv->reorderColumns(['colname1'=>0, 'colname2'=>4]);
// Row reordering
// to move the row with id of 22 to the start
$csv->reorderRow('id', 22, 0);
// or move id 22 to the start, and id 5 after it
$csv->reorderRows('id', [22 => 0, 5 => 1]);
// Sort rows by a column
$csv->reorderRowsByColumn('id', 'desc');
// or even multiples:
$csv->reorderRowsByColumns(['name', 'id' => 'desc']);
// Output
var_dump($parser->toArray($csv));
var_dump($parser->toString($csv));
var_dump($parser->toFile($csv, 'demo.csv')); // file was created?
// Need to chunk into multiple chunks/files?
$chunks = $parser->toChunks($csv, 1000);
foreach ($chunks as $i => $chunk) {
$parser->toFile($chunk, "output-{$i}.csv");
}
// Remove duplicates
$csv->removeDuplicates('email');
// Remove blanks
$csv->removeBlanks('email');
```
## Writing CSV as a Stream
The `writeStream` method allows you to write CSV data as a stream. This can be useful for writing large datasets efficiently.
### Example: Writing to a File
```php
use CsvParser\Parser;
$file = fopen('output.csv', 'w');
$callback = function () {
static $data = [
['name' => 'John', 'age' => 30],
['name' => 'Jane', 'age' => 25],
['name' => 'Doe', 'age' => 40],
];
return array_shift($data);
};
Parser::writeStream($file, ['name', 'age'], $callback);
fclose($file);
```
### Example: Writing to the Screen
```php
use CsvParser\Parser;
$resource = fopen('php://output', 'w');
$callback = function () {
$data = [
['name' => 'John', 'age' => 30],
['name' => 'Jane', 'age' => 25],
['name' => 'Doe', 'age' => 40],
];
foreach ($data as $row) {
yield $row;
}
};
Parser::writeStream($resource, ['name', 'age'], $callback);
fclose($resource);
```
### Example: Writing from a PDO Fetch
```php
use CsvParser\Parser;
use PDO;
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');
$stmt = $pdo->query('SELECT name, age FROM users');
$file = fopen('output.csv', 'w');
Parser::writeStream($file, ['name', 'age'], fn() => $stmt->fetch(PDO::FETCH_NUM));
fclose($file);
```
In this example, the `callback` function uses a PDO statement to fetch rows from a database. The `writeStream` method will continue to call the `callback` until it returns `false`.
## Test
To run the tests, you can use PHPUnit. Make sure you have PHPUnit installed and then run:
```bash
phpunit .
```
## License
[](https://app.fossa.io/projects/git%2Bgithub.com%2Fstilliard%2FCsvParser?ref=badge_large)