okneloper/csv
Composer 安装命令:
composer require okneloper/csv
包简介
OOP wrapper for PHP's csv functions, with column mapping for easy code maintenance.
关键字:
README 文档
README
OOP wrapper for PHP's csv functions, with column mapping for easy code maintenance.
Easy to use and maintain the code using it
The code can be easily adopted to changing requirements and input file formats.
Consider CSV data of the world's heaviest animals having position, animal and weight in 1 line as column headings.
<?php use \Okneloper\Csv\Stream\Input\FileStream; use \Okneloper\Csv\CsvReader; // read CSV data from a file $dataSource = new FileStream($file); // creata a reader $csv = new CsvReader($dataSource); // read rows one by one while ($row = $csv->read()) { echo $row->position; // value for the row in column 0 with header 'position' echo $row->animal; // value for the row in column 1 with header 'animal' echo $row->weight; // value for the row in column 2 with header 'weight' echo $row["weight"]; // also supports array syntax }
Usage
Use a CSV file as a data source
$dataSource = new \Okneloper\Csv\Stream\Input\FileStream($file);
Or alternatively use a string containing CSV data
$dataSource = new \Okneloper\Csv\Stream\StringStream("position,animal,weight\n1,Blue whale,180 tonnes\n2,African Elephant,6350 kg\n3,Brown Bear,1 ton");
Create a reader using the data source
$csv = new \Okneloper\Csv\CsvReader($dataSource);
Read the data
while ($row = $csv->read()) { print_r($row->toArray()); }
Mapped data can be accessed as array elements:
echo $row['position'];
or object properties:
echo $row->position;
Column mapping
Mapping to header
By default, the data is mapped to the header row.
$csv = new \Okneloper\Csv\CsvReader($dataSource);
Outputs
Array
(
[position] => 1
[animal] => Blue whale
[weight] => 180 tonnes
)
...
Custom mapping
If you get a new CSV to process every once in a regularly and the column order or names may change, custom mapping helps to deal with it:
$csv = new \Okneloper\Csv\CsvReader($dataSource, true, ['Nr', 'Who', 'HowMuch']);
Array
(
[Nr] => 1
[Who] => Blue whale
[HowMuch] => 180 tonnes
)
...
No header
$csv = new \Okneloper\Csv\CsvReader($dataSource, false, ['Nr', 'Who', 'HowMuch']);
Array
(
[Nr] => position
[Who] => animal
[HowMuch] => weight
)
Array
(
[Nr] => 1
[Who] => Blue whale
[HowMuch] => 180 tonnes
)
...
No header, no mapping
$csv = new \Okneloper\Csv\CsvReader($dataSource, false, null);
Array ( [0] => position [1] => animal [2] => weight ) Array ( [0] => 1 [1] => Blue whale [2] => 180 tonnes ) ...
okneloper/csv 适用场景与选型建议
okneloper/csv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.77k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 10 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「csv」 「csvreader」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 okneloper/csv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 okneloper/csv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 okneloper/csv 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CSV Reader to array/object iterator with low memory usage and ease of use.
laravel facade to read/write csv file
Collection of tools to use the full power of the Enyalius framework
Fast Excel import/export for Laravel
Spreadsheet reader library for Excel, OpenOffice and structured text files, compatible with cakephp3
A formatting library that converts data output between XML, CSV, JSON, TXT, YAML and a few others.
统计信息
- 总下载量: 4.77k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-25