Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/buibr/csv-helper
Parser/Builder of CSV data to file/file to data
https://github.com/buibr/csv-helper
Last synced: 23 days ago
JSON representation
Parser/Builder of CSV data to file/file to data
- Host: GitHub
- URL: https://github.com/buibr/csv-helper
- Owner: buibr
- Created: 2019-03-06T09:02:37.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-07-12T15:29:49.000Z (6 months ago)
- Last Synced: 2024-11-18T07:06:37.535Z (about 2 months ago)
- Language: PHP
- Size: 53.7 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CSV Helper
CSV Helper is a robust parser/builder for converting CSV data to arrays and vice versa. This tool is designed for handling large CSV datasets efficiently.
## Features
- Parse CSV files into arrays.
- Extract specific columns from CSV files.
- Iterate through CSV records seamlessly.
- Build CSV files from arrays.## Installation
The preferred method of installing this extension is through [Composer](http://getcomposer.org/download/).
You can install the package by running the following command:
```sh
composer require --prefer-dist buibr/csv-helper "^1.5"
```Alternatively, you can add it directly to the `require` section of your `composer.json` file:
```json
"require": {
"buibr/csv-helper": "*"
}
```## Usage
### Parse CSV Data
#### Get Data as Array
To parse a CSV file and get the data as an array:
```php
fromFile()->toArray();
?>
```#### Get Data as Array from File
Alternatively, you can parse the CSV file and directly get the data as an array in a single line:
```php
fromFile('path/to/file')->toArray();
?>
```### Extract Specific Columns
#### Get Only One Column as a One-Dimensional Array
Consider the following `file.csv`:
```csv
name,email,phone
aaa,bbb,ccc
ddd,eee,fff
ggg,hhh,iii
```To extract the `email` column:
```php
fromFile('path/to/file')->toColumn('email');
?>
```Result:
```php
$data = [
0 => "bbb",
1 => "eee",
2 => "hhh"
];
```#### Get Specific Columns (Version 1.5.4+)
To extract multiple columns:
```php
toColumns(['email', 'phone']);
?>
```Result:
```php
$data = [
0 => ["bbb", "ccc"],
1 => ["eee", "fff"],
2 => ["hhh", "iii"]
];
```### Accessing Data
#### Get the First Element
```php
current();
```Result:
```php
Array
(
[0] => John
[1] => Doe
[2] => [email protected]
[3] => 003344003203
[4] => Unknown
)
```#### Get Associative Array
```php
current(true);
```Result:
```php
Array
(
[Firstname] => John
[Lastname] => Doe
[Email] => [email protected]
[Phone] => 003344003203
[Adress] => Unknown
)
```#### Iterate Through CSV Records
```php
valid()) {
// Get item as array
$item = $csv->current(true);// Get the value of the 'Firstname' column from the current record
$name = $csv->column('Firstname');// Get some of the columns
$fullname = implode(' ', $csv->columns(['Firstname', 'Lastname']));$csv->next();
}
```## For More Use Cases
Explore the `test` folder in this repository to see additional examples and use cases.
---
This README provides a detailed guide on installing and using the CSV Helper. If you encounter any issues or have questions, feel free to open an issue on the GitHub repository.