PHP code example of net_bazzline / php_component_csv

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

    

net_bazzline / php_component_csv example snippets


$reader = new Reader('my/file.csv');
//I am using json_encode() since there is no official and best way how to
// output arrays on the command line.

//read one line
echo json_encode($reader->readOne()) . PHP_EOL;

//read 10 lines
foreach ($reader->readMany(10) as $line) {
    echo json_encode($line) . PHP_EOL;
}

//read all lines
foreach ($reader->readAll() as $line) {
    echo json_encode($line) . PHP_EOL;
}

$reader = new Reader('my/file.csv');
//I am using json_encode() since there is no official and best way how to
// output arrays on the command line.

if ($reader->hasHeadline()) {
    echo 'headlines: ' . json_encode($reader->readHeadline());
}

foreach ($reader as $line) {
    echo json_encode($line) . PHP_EOL;
}

$reader = new Reader('my/file.csv');
//I am using json_encode() since there is no official and best way how to
// output arrays on the command line.

while ($line = $reader()) {
    echo json_encode($line) . PHP_EOL;
}

//$headlines contains a php array
//$lines contains a php array of arrays
$writer = new Writer('my/file.csv');

$writer->writeHeadline($headlines);

foreach ($lines as $line) {
    $writer->writeOne($line);
}

//$headlines contains a php array
//$lines contains a php array of arrays
$writer = new Writer('my/file.csv');

$writer->writeHeadline($headlines);
$writer->writeMany($lines);

//$line contains a php array
//$lines contains a php array of arrays
$writer = new Writer('my/file.csv');

$writer($line);

foreach ($lines as $line) {
    $writer($line);
}

$writer = new Writer('my/file.csv');

$writer->truncate();

$writer = new Writer('my/file.csv');

$writer->copy('my/my_first_copy.csv');    //writer will still write into "file.csv"

$writer->copy('my/my_second_copy.csv', true);    //writer will write in "my_second_copy.csv"

$writer = new Writer('my/file.csv');

$writer->move('my/new_name.csv');   //writer will write in "new_name.csv"

mkdir -p vendor/net_bazzline/php_component_csv
cd vendor/net_bazzline/php_component_csv
git clone https://github.com/bazzline/php_component_csv .

composer