定制 heller/simple-csv 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

heller/simple-csv

Composer 安装命令:

composer require heller/simple-csv

包简介

A package to make working with CSV files as convenient and simple as possible

README 文档

README

Make dealing with CSV data as easy and comfortable as possible.

Installation

composer require heller/simple-csv

Usage

Basic array

use Heller\SimpleCsv\Csv;

$csv = Csv::read('filepath.csv')->toArray();

$csv = Csv::read('http://urlto.csv')->toArray();

Header mapping

Assuming that you have a csv table where the first row holds the cullum names using the mapToHeader function makes handling the data much easier.

use Heller\SimpleCsv\Csv;

$csv = Csv::read('filepath.csv')
            ->mapToHeaders()
            ->toArray();

foreach($csv as $row){
    echo $row['columnname']; // instead of using $row[3]
}

Skipping rows & columns

Sometimes it can be helpful to skip certain rows or columns. You can do that using skipRows and skipColumns methods.

$csv = Csv::read('filepath.csv')
           ->skipRows(1)
           ->skipColumns([2, 4, 'columnname'])
           ->toArray();

Both methods accept either an int for a certain row / column or array to skip multiple rows or columns. The skipColumns method also accepts column (header) names as input.

Filtering

As the csv data is just array, we can of course filter the data however you like. For convenience we can also filter while collecting the data using the filter() callback.

$csv = Csv::read('filepath.csv')
           ->filter(fn($row) => $row['column'] != 'foo')
           ->toArray();

Map to object

Mapping rows to objects allows you to work with CSV data in a more structured way. By default the rows are mapped to stdClass which allows to access the CSV data using object properties.

However, if you specify a custom class each row will be mapped to the class based on the header column names.

// ...

$csv->mapToObject(); // Maps rows to stdClass

$csv->mapToObject(CsvRow::class)
    ->filter(function(CsvRow $item){
        return $item->isValid();
    })
    ->toArray();

When using the mapToObject method, the column headers slightly adjusted so the object properties are ensured to have valid names. If a column header was named "Starts At", the name will be normalized to "starts_at" so it can be accessed with $row->starts_at. For custom class mapping the (normalized) column header has to match the class property name.

Individually process row

The get method will allways return an of rows from the csv file. Either as arrays or object. However, while being handy for smaller tasks, this is not memory efficient and can cause problems with large csv files. To solve this, you can process each row individually and still let the package solve the mapping, filtering and skipping etc. This even makes handling large files with millions of records as simple as possible.

$csv->mapToObject(CsvRow::class)
    ->each(function(CsvRow $item) {
        // ... import the data or handle the data however you like
    });

heller/simple-csv 适用场景与选型建议

heller/simple-csv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 11 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 heller/simple-csv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 heller/simple-csv 我们能提供哪些服务?
定制开发 / 二次开发

基于 heller/simple-csv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 24
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-14