PHPackages                             popphp/pop-csv - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PDF &amp; Document Generation](/categories/documents)
4. /
5. popphp/pop-csv

ActiveLibrary[PDF &amp; Document Generation](/categories/documents)

popphp/pop-csv
==============

Pop CSV Component for Pop PHP Framework

4.2.3(3mo ago)47.9k↑38.9%2BSD-3-ClausePHPPHP &gt;=8.3.0CI passing

Since Feb 22Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/popphp/pop-csv)[ Packagist](https://packagist.org/packages/popphp/pop-csv)[ Docs](https://github.com/popphp/pop-csv)[ RSS](/packages/popphp-pop-csv/feed)WikiDiscussions master Synced yesterday

READMEChangelog (10)Dependencies (2)Versions (24)Used By (2)

pop-csv
=======

[](#pop-csv)

[![Build Status](https://github.com/popphp/pop-csv/workflows/phpunit/badge.svg)](https://github.com/popphp/pop-csv/actions)[![Coverage Status](https://camo.githubusercontent.com/c6672313cc143285b026350b35720a5965450398716446cb98dec46ff483f227/687474703a2f2f63632e706f707068702e6f72672f636f7665726167652e7068703f636f6d703d706f702d637376)](http://cc.popphp.org/pop-csv/)

[![Join the chat at https://discord.gg/TZjgT74U7E](https://camo.githubusercontent.com/acad7b0eeb78b78d08ffd2b85681ab243436388b5f86f8bcb956a69246e53739/68747470733a2f2f6d656469612e706f707068702e6f72672f696d672f646973636f72642e737667)](https://discord.gg/TZjgT74U7E)

- [Overview](#overview)
- [Install](#install)
- [Quickstart](#quickstart)
- [Options](#options)
- [Output CSV](#output-csv)
- [Append Data](#append-data)

Overview
--------

[](#overview)

`pop-csv` provides a streamlined way to work with PHP data and the CSV format.

It is a component of the [Pop PHP Framework](https://www.popphp.org/).

Install
-------

[](#install)

Install `pop-csv` using Composer.

```
composer require popphp/pop-csv

```

Or, require it in your composer.json file

```
"require": {
    "popphp/pop-csv" : "^4.2.1"
}

```

[Top](#pop-csv)

Quickstart
----------

[](#quickstart)

### Create a CSV string

[](#create-a-csv-string)

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

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

The $csvString variable now contains:

```
first_name,last_name
Bob,Smith
Jane,Smith

```

### Create data from a CSV string

[](#create-data-from-a-csv-string)

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\Csv\Csv($csvString);
$phpData = $csv->unserialize();
```

[Top](#pop-csv)

Options
-------

[](#options)

Where serializing or unserializing CSV data, there are a set of options available to tailor the process:

```
$options = [
    'exclude'   => ['id'],    // An array of fields to exclude
    'include'   => ['email'], // An array of fields to explicitly include, omitting all others
    'delimiter' => ',',       // Delimiter defaults to ',' - could be "\t" or something 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
];
```

**Map/Columns Example**

```
$users = [
    'id'       => 1,
    'username' => 'testuser',
    'country'  => [
        'name' => 'United States',
        'code' => 'US'
    ],
    'roles'    => [
        ['id' => 1, 'name' => 'Admin'],
        ['id' => 2, 'name' => 'Staff'],
    ]
];

```

```
$options = [
    'map' => [
        'country' => 'code'
    ],
    'columns' => [
        'roles' => 'name'
    ]
]

```

Pass the options array to constructor method:

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

The above will output the following CSV data:

```
id,username,country,roles
1,testuser,US,"Admin,Staff"

```

[Top](#pop-csv)

Output CSV
----------

[](#output-csv)

### Write to File

[](#write-to-file)

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

### Output to HTTP

[](#output-to-http)

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

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

##### Force download of file

[](#force-download-of-file)

Pass a `true` boolean as the second parameter, which forces `attachment` for the `Content-Disposition` header.

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

##### Additional HTTP headers

[](#additional-http-headers)

Additional HTTP headers can be passed to the third parameter:

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

[Top](#pop-csv)

Append Data
-----------

[](#append-data)

In the case of working with large data sets, you can append CSV data to an existing file on disk. This prevents loading large amounts of data into memory that may exceed the PHP environment's limits.

```
us Pop\Csv\Csv;

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

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

[Top](#pop-csv)

###  Health Score

57

—

FairBetter than 98% of packages

Maintenance82

Actively maintained with recent releases

Popularity29

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity88

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~184 days

Recently: every ~121 days

Total

19

Last Release

94d ago

Major Versions

3.2.1 → 4.0.02023-11-09

PHP version history (7 changes)3.0.0PHP &gt;=5.6.0

3.0.2PHP &gt;=7.1.0

3.2.0PHP &gt;=7.3.0

3.2.1PHP &gt;=7.4.0

4.0.0PHP &gt;=8.1.0

4.1.1PHP &gt;=8.2.0

4.2.1PHP &gt;=8.3.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/c19dee900e9e20039c723cc403f76b5c22ac2dddb3f86773ae64fb082d4949e2?d=identicon)[nicksagona](/maintainers/nicksagona)

---

Top Contributors

[![nicksagona](https://avatars.githubusercontent.com/u/898670?v=4)](https://github.com/nicksagona "nicksagona (49 commits)")

---

Tags

phpcsvpoppop php

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/popphp-pop-csv/health.svg)

```
[![Health](https://phpackages.com/badges/popphp-pop-csv/health.svg)](https://phpackages.com/packages/popphp-pop-csv)
```

###  Alternatives

[openspout/openspout

PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way

1.2k70.2M243](/packages/openspout-openspout)[popphp/pop-pdf

PHP PDF library for generating and importing PDF documents. A component of the Pop PHP Framework

208.8k1](/packages/popphp-pop-pdf)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
