PHP code example of gugglegum / csv-rw

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

    

gugglegum / csv-rw example snippets


use \gugglegum\CsvRw\CsvReader;
use \gugglegum\CsvRw\CsvFormat;

$csv = new CsvReader(new CsvFormat([
    'delimiter' => ',',
    'enclosure' => '"',
    'escape' => '\\',
]));
$csv->open('input.csv', CsvReader::WITH_HEADERS);

foreach ($csv as $row) {
    var_dump($row);
}

$csv->close();

use gugglegum\CsvRw\CsvFormat;
use gugglegum\CsvRw\CsvWriter;

$headers = ['id', 'firstName', 'lastName'];

$rows = [
    [
        'id' => 1,
        'firstName' => 'John',
        'lastName' => 'Smith',
    ],
];

$csv = new CsvWriter(new CsvFormat([
   'delimiter' => ',',
   'enclosure' => '"',
   'escape' => '\\',
]));

$csv->open('output.csv', CsvWriter::WITH_HEADERS, $headers);

foreach ($rows as $row) {
    $csv->writeRow($row);
}

$csv->close();

ini_set('auto_detect_line_endings', true);