PHP code example of popphp / pop-csv

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

    

popphp / pop-csv example snippets


$phpData = [
    [
        'first_name' => 'Bob',
        'last_name'  => 'Smith'
    ],
    [
        'first_name' => 'Jane',
        'last_name'  => 'Smith'
    ]
];

$data      = new Pop\Csv\Csv($phpData);
$csvString = $data->serialize();

$csv     = new Pop\Csv\Csv($csvString);
$phpData = $csv->unserialize();

$csv = new Pop\Csv\Csv($phpData);

$csv->getData();          // the PHP array, or null if it hasn't been set/unserialized
$csv->getString();        // the CSV string, or null if it hasn't been set/serialized
$csv->setData($otherData);
$csv->setString($otherCsvString);

$csv->isUnserialized();   // true once $data is populated
$csv->isSerialized();     // true once $string is populated

$csv = new Pop\Csv\Csv($phpData);
echo $csv; // same as echo $csv->serialize();

use Pop\Csv\Csv;

$csv     = Csv::loadFile('/path/to/file.csv');        // new Csv($file) + unserialize(), returns the Csv object
$csv     = Csv::loadString($csvString);               // same, from a raw CSV string
$csv     = Csv::loadData($phpData);                   // new Csv($data) + serialize(), returns the Csv object
$phpData = Csv::getDataFromFile('/path/to/file.csv'); // like loadFile(), but returns the array directly

Csv::writeDataToFile($phpData, '/path/to/file.csv');  // build + serialize + writeToFile in one call
Csv::outputDataToHttp($phpData, null, 'my-file.csv'); // build + serialize + outputToHttp in one call

$options = [
    'exclude'        => ['id'],    // An array of fields to exclude
    'mething else
    'enclosure'      => '"',       // Default string enclosure, i.e. "my data","other data"
    'escape'         => '"',       // String character to escape in the data, i.e. "my ""data"" here"
    'fields'         => true,      // Include the field names in the first row 
    'newline'        => true,      // Allow newlines in a data cell. Set as false to trim them
    'limit'          => 0,         // Character limit of a data cell when serializing. 0 means no limit
    'length'         => 0,         // Max line length read per row when parsing (fgetcsv). 0 means no limit
    'map'            => [],        // Array key of a single array value to map to the data cell value
    'columns'        => [],        // Array key of a multidimensional array value to map and join into the data cell value
    'escapeFormulas' => false,     // Guard against CSV/Excel formula injection by prefixing risky cells with a single quote
];

$data      = new Pop\Csv\Csv($users, $options);
$csvString = $data->serialize();
echo $csvString;

$options = ['escapeFormulas' => true];
$data    = [['note' => '=SUM(A1:A10)']];
$csv     = new Pop\Csv\Csv($data, $options);
echo $csv->serialize(); // note\n'=SUM(A1:A10)\n

$csv = new Pop\Csv\Csv($phpData, ['delimiter' => "\t"]);
$csv->writeToFile('/path/to/file.tsv');

$loaded  = Csv::loadFile('/path/to/file.tsv', ['delimiter' => "\t"]);
$phpData = $loaded->getData();

$phpData = [
    [
        'first_name' => 'Bob',
        'last_name'  => 'Smith'
    ],
    [
        'first_name' => 'Jane',
        'last_name'  => 'Smith'
    ]
];

$data = new Pop\Csv\Csv($phpData);
$data->writeToFile('/path/to/file.csv');

$phpData = [
    [
        'first_name' => 'Bob',
        'last_name'  => 'Smith'
    ],
    [
        'first_name' => 'Jane',
        'last_name'  => 'Smith'
    ]
];

$data = new Pop\Csv\Csv($phpData);
$data->outputToHttp('my-file.csv');

$data->outputToHttp('my-file.csv', false);

$data->outputToHttp('my-file.csv', false, ['X-Header' => 'some-header-value']);

use Pop\Csv\Csv;

$phpData = [
    ['first_name' => 'Bob', 'last_name' => 'Smith']
];

// Write just the header row to a file
$csv = new Pop\Csv\Csv($phpData);
$csv->writeBlankFile('/path/to/template.csv');

// Or output it straight to HTTP as a download
$csv->outputBlankFileToHttp('template.csv');

// One-liner static equivalents that don't 

use Pop\Csv\Csv;

$phpData = [
    [
        'first_name' => 'Bob',
        'last_name'  => 'Smith'
    ],
    [
        'first_name' => 'Jane',
        'last_name'  => 'Smith'
    ]
];

Csv::appendDataToFile('my-file.csv', $phpData);

Csv::appendRowToFile('my-file.csv', ['first_name' => 'John', 'last_name' => 'Smith']);

$csv = new Pop\Csv\Csv(null, ['delimiter' => "\t"]);
$csv->appendRow('my-file.tsv', ['first_name' => 'John', 'last_name' => 'Smith']);

Csv::appendRowToFile('my-file.csv', $row, [], false);

foreach (Csv::readRowsFromFile('my-large-file.csv') as $row) {
    // $row is processed one at a time; memory usage stays flat
    // regardless of how many rows the file has
}

$rowCount = Csv::getRowCountFromFile('my-large-file.csv', ['headers' => true]);

if (Csv::isValid($string)) {
    $phpData = Csv::loadString($string)->getData();
}

try {
    Csv::appendRowToFile('my-file.csv', $row);
} catch (Pop\Csv\Exception $e) {
    // handle a missing file or a column mismatch
}