PHP code example of buibr / csv-helper

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

    

buibr / csv-helper example snippets


 
use buibr\csvhelper\CsvParser;

$parser = new CsvParser('path/to/file');
$data   = $parser->fromFile()->toArray();

 
use buibr\csvhelper\CsvParser;

$data = (new CsvParser)->fromFile('path/to/file')->toArray();


use buibr\csvhelper\CsvParser;

$data = (new CsvParser)->fromFile('path/to/file')->toColumn('email');

$data = [
    0 => "bbb",
    1 => "eee",
    2 => "hhh"
];


use buibr\csvhelper\CsvParser;

$data = (new CsvParser('path/to/file'))->toColumns(['email', 'phone']);

$data = [
    0 => ["bbb", "ccc"],
    1 => ["eee", "fff"],
    2 => ["hhh", "iii"]
];


use buibr\csvhelper\CsvParser;

$csv    = new CsvParser('path/to/file');
$first  = $csv->current();

Array
(
    [0] => John
    [1] => Doe
    [2] => [email protected]
    [3] => 003344003203
    [4] => Unknown
)


use buibr\csvhelper\CsvParser;

$assoc  = $csv->current(true);

Array
(
    [Firstname] => John
    [Lastname] => Doe
    [Email] => [email protected]
    [Phone] => 003344003203
    [Adress] => Unknown
)


use buibr\csvhelper\CsvParser;

$csv = new CsvParser('path/to/file');

while ($csv->valid()) {
    // Get item as array
    $item = $csv->current(true);

    // Get the value of the 'Firstname' column from the current record
    $name = $csv->column('Firstname');

    // Get some of the columns
    $fullname = implode(' ', $csv->columns(['Firstname', 'Lastname']));

    $csv->next();
}