PHP code example of miquido / csv-file-reader

1. Go to this page and download the library: Download miquido/csv-file-reader 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/ */

    

miquido / csv-file-reader example snippets




use Miquido\CsvFileReader\CsvFile;
use Miquido\CsvFileReader\Line\CsvLineInterface;

// open a file
$csv = new CsvFile('./examples/users.csv');

$count = $csv->countLines(); // 101

// don't worry about memory, it reads a file line-by-line 
foreach ($csv->readLines() as $line) {
    /** @var CsvLineInterface $line */
    $line->getLineNumber(); // 2 ... 101
    $line->getData(); // ['id' => '1', 'name' => 'Miriam', 'surname' => 'Mccoy', 'age' => '79'] ...
}



use Miquido\CsvFileReader\CsvFile;
use Miquido\CsvFileReader\CsvFileReader;
use Miquido\CsvFileReader\Line\CsvLineInterface;

$reader = new CsvFileReader(new CsvFile('./examples/users.csv'));
$reader->lines()->subscribe(function (CsvLineInterface $line) {
    // do something with a line
    $line->getLineNumber(); // 2 ... 101
    $line->getData(); // ['id' => '1', 'name' => 'Miriam', 'surname' => 'Mccoy', 'age' => '79'] ...
});

$reader->loop(); // start reading a file



use Miquido\CsvFileReader\CsvFile;
use Miquido\CsvFileReader\CsvFileReader;
use Miquido\CsvFileReader\Line\CsvLineInterface;
use Miquido\DataStructure\Map\MapInterface;
use Miquido\DataStructure\Map\Map;

// change data to Map object
$transformer = function (array $data, int $line): MapInterface {
    return new Map($data);
};

$reader = new CsvFileReader(new CsvFile('./examples/users.csv'), $transformer);
$reader->lines()->subscribe(function (CsvLineInterface $line): void {
    $line->getData(); // getData() now returns Map() object
});

$reader->loop(); // start reading a file




use Miquido\CsvFileReader\CsvFile;
use Miquido\CsvFileReader\CsvFileReader;
use Miquido\CsvFileReader\Exception\InvalidCsvLineException;

// check user's age
$transformer = function (array $data, int $line): array {
    $age = (int) $data['age'];
    if ($age < 18) {
        throw new \Exception('Invalid age');
    }
    
    return $data;
};

$reader = new CsvFileReader(new CsvFile('./examples/users.csv'), $transformer);
$reader->data()->subscribe(function (array $lineData): void {
    // do something with data
});
$reader->errors()->subscribe(function (InvalidCsvLineException $e): void {
    // do something with an error
    $e->getMessage();
    $e->getCsvLine();
});

$reader->loop(); // start reading a file



use Miquido\CsvFileReader\CsvFile;
use Miquido\CsvFileReader\CsvFileReader;
use Miquido\Observable\Operator;

$batchSize = 10;

$reader = new CsvFileReader(new CsvFile('./examples/users.csv'));
$reader->lines()->pipe(new Operator\BufferCount($batchSize))->subscribe(function (array $lines): void {
    // do something with 10 lines
});

$reader->loop(); // start reading a file