PHP code example of phpexperts / csv-speaker

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

    

phpexperts / csv-speaker example snippets


    // The CSVReader can be instantiated from a file via just the file's filename:
    $csvReader = CSVReader::fromFile('/tmp/my.csv');

    // or via a SplFileObject:
    $csvReader = CSVReader::fromFile(new SplFileObject('/tmp/my.csv'));

    // More simply, you can instantiate from any CSV string.
    $csvReader = CSVReader::fromString('a,b,c,d');

    $output = $csvReader->toArray();
    /* [
        ['a', 'b', 'c', 'd']
    ] */

    $csv = <<<CSV
    "First Name","Last Name",Age
    "John","Galt",37
    CSV;

    $output = CSVReader::fromString($csv)->toArray();
    /* [
        ['First Name' => 'John', 'Last Name' => 'Galt', 'Age' => 37]
    ] */

    $csv = <<<CSV
    "First Name","Last Name",Age
    "John","Galt",37
    CSV;

    $output = CSVReader::fromString($csv, false)->toArray();
    /* [
        ['First Name', 'Last Name', 'Age'],
        ['John',       'Galt',      '37']
    ] */

    $input = [
        ['a', 'b', 'c'],
        ['d', 'e', 'f']
    ];
    $csvWriter = new CSVWriter();
    $csvWriter->addRow($input[0]);
    $csvWriter->addRow($input[1]);
    $csv = $csvWriter->getCSV();

    /* csv:
        a,b,c
        d,e,f
    */

    $input = [
        ['Name' => 'John Galt', 'Age' => 37],
        ['Name' => 'Mary Jane', 'Age' => 27],
    ];
    $csvWriter = new CSVWriter();
    $csvWriter->addRow($input[0]);
    $csvWriter->addRow($input[1]);
    $csv = $csvWriter->getCSV();

    /* csv:
        Name,Age
        "John Galt",37
        "Mary Jane",27
    */