PHP code example of keboola / csv

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

    

keboola / csv example snippets


$csvFile = new Keboola\Csv\CsvReader(__DIR__ . '/_data/test-input.csv');
foreach($csvFile as $row) {
	var_dump($row);
}

$csvFile = new \Keboola\Csv\CsvFile(
    $fileName,
    CsvFile::DEFAULT_DELIMITER,
    CsvFile::DEFAULT_ENCLOSURE,
    CsvFile::DEFAULT_ESCAPED_BY,
    1
)
foreach($csvFile as $row) {
	var_dump($row);
}

$csvFile = new Keboola\Csv\CsvWriter(__DIR__ . '/_data/test-output.csv');
$rows = [
	[
		'col1', 'col2',
	],
	[
		'first column', 'second column',
	],
];

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

$fileName = __DIR__ . '/_data/test-output.csv';
$file = fopen($fileName, 'a');
$csvFile = new Keboola\Csv\CsvWriter($file);
$rows = [
	[
		'col1', 'col2',
	],
	[
		'first column', 'second column',
	],
];

foreach ($rows as $row) {
	$csvFile->writeRow($row);
}
fclose($file);

$csvFile = new Keboola\Csv\CsvWriter(
    'test-output.csv',
    CsvWriter::DEFAULT_DELIMITER,
    CsvWriter::DEFAULT_ENCLOSURE,
    "\r\n"
)
$rows = [
	[
		'col1', 'col2',
	],
	[
		'first column', 'second column',
	],
];

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

git clone https://github.com/keboola/php-csv.git
cd php-csv
docker-compose build
docker-compose run --rm dev composer install --no-scripts