Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/g105b/phpcsv
Wraps SplFileObject's CSV parsing capabilities with a more human approach
https://github.com/g105b/phpcsv
Last synced: 2 days ago
JSON representation
Wraps SplFileObject's CSV parsing capabilities with a more human approach
- Host: GitHub
- URL: https://github.com/g105b/phpcsv
- Owner: g105b
- License: mit
- Created: 2015-02-07T10:58:31.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-06-06T15:35:48.000Z (over 8 years ago)
- Last Synced: 2024-09-16T19:24:04.316Z (17 days ago)
- Language: PHP
- Homepage:
- Size: 187 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# phpcsv
Wraps SplFileObject's CSV capabilities with a more human approach[![Build status](https://img.shields.io/circleci/project/g105b/phpcsv.svg?style=flat-square)](https://circleci.com/gh/g105b/phpcsv)
[![Composer Version](http://img.shields.io/packagist/v/g105b/phpcsv.svg?style=flat-square)](https://packagist.org/packages/g105b/phpcsv)
[![Download Stats](http://img.shields.io/packagist/dm/g105b/phpcsv.svg?style=flat-square)](https://packagist.org/packages/g105b/phpcsv)## Features at a glance
* Enhances PHP's SplFileObject, a memory-efficient file stream.
* Simple filtering of rows by field value (`getAllBy("fieldName", "fieldValue")`).
* Results are associative arrays, the indices are the CSV header names.
* Iterate over CSV files by row.
* Reference CSV rows by row number.
* Reference CSV rows by ID value.## Screenshot in action
![Screenshot of phpcsv](https://raw.githubusercontent.com/g105b/phpcsv/master/screenshot.png)
## Usage
Here are a few use cases to best show the functionality of the library. For a complete guide, [visit the documentation](https://github.com/g105b/phpcsv/wiki).
### Add rows
```php
$csv = new Csv("/path/to/file.csv");
$csv->add([
"firstName" => "Alan",
"lastName" => "Statham",
"Job Title" => "Consultant Radiologist",
]);
$csv->add([
"firstName" => "Caroline",
"lastName" => "Todd",
"Job Title" => "Surgical Registrar",
]);
```### Get rows
```php
$csv = new Csv("/path/to/file.csv");
$resultRows = $csv->getAllBy("gender", "F"); // array of all matching rows.
$firstRow = $csv->getBy("gender", "F"); // single row, first matching.
```### Iterate over rows
```php
$csv = new Csv("/path/to/file.csv");foreach ($csv as $rowNumber => $row) {
// $row is an associative array with CSV headers as each key.
// $rowNumber starts from 1 (ignoring header row).
}
```### Update row
```php
$csv = new Csv("/path/to/file.csv");
$row = $csv->getBy("email", "[email protected]");// Update the matching row with provided fields, keeping any
// existing field data on the existing row.
$csv->update($row, [
"dateOfBirth" => "1961-08-04",
]);
```### Delete row
```php
$csv = new Csv("/path/to/file.csv");// Delete a row by its index.
$csv->deleteRow(22);
```## Future feature ideas
* Requesting only certain fields in result (v2)
* Type handling (v3)
* Sorting (v4)
* Faster retrieval of indexed rows (v4)