PHP code example of clue / reactphp-csv

1. Go to this page and download the library: Download clue/reactphp-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/ */

    

clue / reactphp-csv example snippets


$stdin = new React\Stream\ReadableResourceStream(STDIN);

$csv = new Clue\React\Csv\Decoder($stdin);

$csv->on('data', function (array $data) {
    // $data is a parsed element from the CSV stream
    // line 1: $data = array('test', '1', '24');
    // line 2: $data = array('hello world', '2', '48');
    var_dump($data);
});

$csv = new Clue\React\Csv\Decoder($stdin, ';');

$csv->on('data', function (array $data) {
    // CSV fields will now be delimited by semicolon
});

$csv = new Clue\React\Csv\Decoder($stdin, ',', '"', '\\', 64 * 1024);

$csv->on('error', function (Exception $error) {
    // an error occurred, stream will close next
});

$csv->on('end', function () {
    // stream successfully ended, stream will close next
});

$csv->on('close', function () {
    // stream closed
    // possibly after an "end" event or due to an "error" event
});

$csv->close();

$csv->pipe($logger);

$stdin = new React\Stream\ReadableResourceStream(STDIN);

$csv = new Clue\React\Csv\AssocDecoder($stdin);

$csv->on('data', function (array $data) {
    // $data is a parsed element from the CSV stream
    // line 1: $data = array('name' => 'test', 'id' => '1');
    // line 2: $data = array('name' => 'hello world', 'id' => '2');
    var_dump($data);
});

$csv->on('headers', function (array $headers) {
    // header line: $headers = array('name', 'id');
    var_dump($headers);
});

$stdout = new React\Stream\WritableResourceStream(STDOUT);

$csv = new Clue\React\Csv\Encoder($stdout);

$csv->write(array('test', true, 24));
$csv->write(array('hello world', 2, 48));

$csv = new Clue\React\Csv\Encoder($stdout, ';');

$csv->write(array('hello', 'world'));

$csv->on('error', function (Exception $error) {
    // an error occurred, stream will close next
});

$csv->on('close', function () {
    // stream closed
    // possibly after an "end" event or due to an "error" event
});

$csv->end();

$csv->close();