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();

$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. 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
];

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

$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', true);

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

us Pop\Csv\Csv;

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

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