tleckie/csv

1.0.1 2021-05-06 14:55 UTC

This package is auto-updated.

Last update: 2024-12-11 15:54:19 UTC


README

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"]));