PHP code example of lazier / csv

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

    

lazier / csv example snippets




use Lazier\Csv\CsvFile;

foreach(CsvFile::createFromString("id,name\n1,foo\n2,bar") as $row => $data) {
    echo $data['id'] . ': ' . $data['name'] . PHP_EOL;
}

// will output:
// 1: foo
// 2: bar



use Lazier\Csv\CsvFile;

foreach(CsvFile::createFromFile('example.csv', useHeaderRow: false) as $data) {
    echo $data[0] . ': ' . $data[1];
}



use Lazier\Csv\CsvFile;

$csvFile = CsvFile::createFromArray([
    ['id' => '1', 'name' => 'Nina'],
    ['id' => '2', 'name' => 'Angela'],
]);

$csvFile->add(['id' => '3', 'name' => 'John']);

// You can save your modified CSV file this way:
$csvFile->saveAs('names.csv');

// You can also output the CSV contents this way:
echo $csvFile->asString();