PHP code example of tleckie / csv

1. Go to this page and download the library: Download tleckie/csv library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tleckie / csv example snippets




leckie\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);
} 


use Tleckie\Csv\Reader;

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


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



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