heller/simple-csv 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱: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
    });

统计信息

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

GitHub 信息

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

其他信息

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