Skip to content

Latest commit

 

History

History
92 lines (61 loc) · 1.87 KB

README.md

File metadata and controls

92 lines (61 loc) · 1.87 KB

PHP csv reader and writer.

Scrutinizer Code Quality Code Intelligence Status Build Status

Installation

You can install the package via composer:

composer require tleckie/csv

Usage

Reader:

<?php

include_once "vendor/autoload.php";

use Tleckie\Csv\Csv;

$csv = new Csv('file.csv');

$reader = $csv->reader();
foreach($reader as $position => $row){
    
    foreach($row as $index => $value){
        // ...
    }
    
    // byIndex
    $row->byIndex(0);
    // hasIndex
    $row->hasIndex(0);
    // new instance with reversed data  
    $row->reverse();
    // array
    $row->toArray();
    // preserve keys
    $row->removeByIndex(1);
    // removeFirst and preserve keys
    $row->removeFirst();
    // removeLast
    $row->removeLast();
    // countable count($row) or $row->count()
    $row->count(); count($row);
} 

Explicit reader

use Tleckie\Csv\Reader;

$csv = new Reader('file.csv', ',', '|');

Writer

use Tleckie\Csv\Csv;
use Tleckie\Csv\Row;

$csv = new Csv('file.csv', ',', '"');

$writer = $csv->writer();

$writer->writeLine([1,2,3,4,5,"Test comma, separated"]);
$writer->writeLine(new Row([1,2,3,4,5,"Test comma, separated"]));

Explicit writer

use Tleckie\Csv\Writer;
use Tleckie\Csv\Row;

$writer = new Writer('file.csv', ',', '|','\\', 'w');

$writer->writeLine([1,2,3,4,5,"Test comma, separated"]);
$writer->writeLine(new Row([1,2,3,4,5,"Test comma, separated"]));