buibr/csv-helper
Composer 安装命令:
composer require buibr/csv-helper
包简介
Parser/Builder of CSV data to file/file to data
README 文档
README
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.
You can install the package by running the following command:
composer require --prefer-dist buibr/csv-helper "^1.5"
Alternatively, you can add it directly to the require section of your composer.json file:
"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 use buibr\csvhelper\CsvParser; $parser = new CsvParser('path/to/file'); $data = $parser->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 use buibr\csvhelper\CsvParser; $data = (new CsvParser)->fromFile('path/to/file')->toArray(); ?>
Extract Specific Columns
Get Only One Column as a One-Dimensional Array
Consider the following file.csv:
name,email,phone aaa,bbb,ccc ddd,eee,fff ggg,hhh,iii
To extract the email column:
<?php use buibr\csvhelper\CsvParser; $data = (new CsvParser)->fromFile('path/to/file')->toColumn('email'); ?>
Result:
$data = [ 0 => "bbb", 1 => "eee", 2 => "hhh" ];
Get Specific Columns (Version 1.5.4+)
To extract multiple columns:
<?php use buibr\csvhelper\CsvParser; $data = (new CsvParser('path/to/file'))->toColumns(['email', 'phone']); ?>
Result:
$data = [ 0 => ["bbb", "ccc"], 1 => ["eee", "fff"], 2 => ["hhh", "iii"] ];
Accessing Data
Get the First Element
<?php use buibr\csvhelper\CsvParser; $csv = new CsvParser('path/to/file'); $first = $csv->current();
Result:
Array ( [0] => John [1] => Doe [2] => johndoe@test.test [3] => 003344003203 [4] => Unknown )
Get Associative Array
<?php use buibr\csvhelper\CsvParser; $assoc = $csv->current(true);
Result:
Array ( [Firstname] => John [Lastname] => Doe [Email] => johndoe@test.test [Phone] => 003344003203 [Adress] => Unknown )
Iterate Through CSV Records
<?php use buibr\csvhelper\CsvParser; $csv = new CsvParser('path/to/file'); while ($csv->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.
buibr/csv-helper 适用场景与选型建议
buibr/csv-helper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.75k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 04 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「csv」 「import」 「extension」 「parse」 「export」 「csv to array」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 buibr/csv-helper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 buibr/csv-helper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 buibr/csv-helper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
Parse use statements for a reflection object
Tool for copying data from a production database to a dev database. Also useful for making backups of production databases.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
统计信息
- 总下载量: 8.75k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 20
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-4-Clause
- 更新时间: 2019-04-10