pop-csv

The popphp/pop-csv component is a component for managing CSV data and files.

Installation

Install it directly into your project:

composer require popphp/pop-csv

Or, include it in your composer.json file:

{
    "require": {
        "popphp/pop-csv": "3.0.*",
    }
}

Basic Use

The popphp/pop-csv component provides a streamlined way to work with PHP data and the CSV format.

Serialize Data

To serialize the data into one of the data types, you can create a data object and call the serialize() method:

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

$data = new Pop\Data\Data($phpData);

$csvString   = $data->serialize();

The $csvString variable now contains:

first_name,last_name
Bob,Smith
Jane,Smith

Unserialize Data

You can either pass the data object a direct string of serialized data or a file containing a string of serialized data. It will detect which one it is and parse it accordingly.

$csv = new Pop\Data\Data($csvString);
// OR
$csv = new Pop\Data\Data('/path/to/file.csv');

$phpData = $csv->unserialize();

Write to File

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

Output to HTTP

$data = new Pop\Data\Data($phpData);
$data->serialize();
$data->outputToHttp();

If you want to force a download, you can set that parameter:

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