phillipsdata/csv
最新稳定版本:1.1.0
Composer 安装命令:
composer require phillipsdata/csv
包简介
CSV Reader and Writer
关键字:
README 文档
README
A CSV reader and writer with no external dependencies, that allows just in time formatting and filtering, and operates on files as well as streams.
Install
composer require phillipsdata/csv
Basic Usage
Reading a CSV
<?php use PhillipsData\Csv\Reader; // Set the input for the reader $reader = Reader::input(new SplFileObject('php://stdin')); $lines = []; // Fetch the result for each line read foreach ($reader->fetch() as $line) { $lines[] = $line; }
Writing a CSV
<?php use PhillipsData\Csv\Writer; $writer = Writer::output(new SplFileObject('/path/to/file.csv')); $data = [ ['colA', 'colB'], ['A1', 'B1'], ['A2', 'B2'] ]; // Write all rows (works great with Iterators) $writer->write($data); // Or, write a single row at a time foreach ($data as $row) { $writer->writeRow($row); }
Factories
<?php use PhillipsData\Csv\Factory; // returns \PhillipsData\Csv\Writer $writer = Factory::writer('path/to/file', ',', '"', '\\'); // returns \PhillipsData\Csv\Reader $reader = Factory::reader('path/to/file', ',', '"', '\\');
Formatting and Filtering
Formatting and Filtering work for both reading and writing.
To format data while iterating, simply specify the format callback function.
// The formatter is called for each line parsed $reader->format(function ($line, $key, $iterator) { return [ 'first_name' => $line['First Name'], 'last_name' => $line['Last Name'], 'email' => strtolower($line['Email']), 'date' => date('c', strtotime($line['Date'])) ]; }); foreach ($reader->fetch() as $line) { // $line now contains 'first_name', 'last_name', 'email', and 'date'. }
To filter data while iterating, simply specify the filter callback function.
// The formatter is called for each line parsed $reader->filter(function ($line, $key, $iterator) { return $line['Last Name'] === 'Smith'; }); foreach ($reader->fetch() as $line) { // $line only contains records where $line['Last Name'] === 'Smith' }
统计信息
- 总下载量: 745
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-02-23