chrgriffin/collection-macro-csv
Composer 安装命令:
composer require chrgriffin/collection-macro-csv
包简介
Laravel Collection macro: convert a collection to a CSV-ready array or string.
README 文档
README
Laravel Collection Macros: CSV
This package will add two Collection macros to transform your Collection into a CSV-ready array or a CSV string, respectively.
Installation
Install in your Laravel project via composer:
composer install chrgriffin/collection-macro-csv
If your version of Laravel supports auto-discovery (versions 5.5 and up), that's it!
For older versions of Laravel, you will need to edit your config/app.php file to include the service provider in your providers array:
return [ // ... 'providers' => [ // ... CollectionMacroCsv\ServiceProvider::class ] ];
Usage
You should now be able to chain ->toCsvArray() and ->toCsvString on any Collection.
toCsvArray
->toCsvArray() will transform a two dimensional Collection of associative arrays into a two dimensional array of 'rows' ready for CSV formatting.
$associativeArray = [ [ 'name' => 'Geralt of Rivia', 'occupation' => 'Witcher' ], [ 'name' => 'Yennefer of Vengerberg', 'occupation' => 'Sorceress' ] ]; $csvArray = collect($associativeArray)->toCsvArray(); /* * [ * ['name', 'occupation'], * ['Geralt of Rivia', 'Witcher'], * ['Yennefer of Vengerberg', 'Sorceress'] * ] */
toCsvString
->toCsvString() will transform a two dimensional Collection of arrays into a CSV string.
$associativeArray = [ [ 'name' => 'Geralt of Rivia', 'occupation' => 'Witcher' ], [ 'name' => 'Yennefer of Vengerberg', 'occupation' => 'Sorceress' ] ]; $csvString = collect($associativeArray)->toCsvString(); // name,occupation\nGeralt of Rivia,Witcher\nYennefer of Vengerberg,Sorceress
You can also pass in a custom delimiter if you need to use something other than a comma:
$csvString = collect($associativeArray)->toCsvString('|'); // name|occupation\nGeralt of Rivia|Witcher\nYennefer of Vengerberg|Sorceress
Inconsistent Columns
For Collections of arrays where the array indexes are not consistent with one another, a default value of null will be used to 'fill in' missing values:
$associativeArray = [ [ 'name' => 'Geralt of Rivia', 'occupation' => 'Witcher', 'witcher_school' => 'Wolf' ], [ 'name' => 'Yennefer of Vengerberg', 'occupation' => 'Sorceress', 'magic_speciality' => 'Portals' ] ]; $csvArray = collect($associativeArray)->toCsvArray(); /* * [ * ['name', 'occupation', 'witcher_school', 'magic_speciality'], * ['Geralt of Rivia', 'Witcher', 'Wolf', null], * ['Yennefer of Vengerberg', 'Sorceress', null, 'Portals'] * ] */ $csvString = collect($associativeArray)->toCsvString(); // name,occupation,witcher_school,magic_speciality\nGeralt of Rivia,Witcher,Wolf,\nYennefer of Vengerberg,Sorceress,,Portals
Structure Requirements
The only requirement is that your Collection contain only arrays. If a non-array value is encountered at the first level of the Collection, a MalformedCollectionException will be thrown.
$malformedArray = [ [ 'name' => 'Geralt of Rivia', 'occupation' => 'Witcher', 'witcher_school' => 'Wolf' ], 'Yennefer of Vengerberg' ]; // throws a MalformedCollectionException $csvArray = collect($malformedArray)->toCsvArray();
chrgriffin/collection-macro-csv 适用场景与选型建议
chrgriffin/collection-macro-csv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 03 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 chrgriffin/collection-macro-csv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chrgriffin/collection-macro-csv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-03