承接 okneloper/csv 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

okneloper/csv

最新稳定版本:v0.5.2

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
)
...

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-10-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固