PHPackages                             jletrondo/csv-writer - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. jletrondo/csv-writer

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jletrondo/csv-writer
====================

A simple CSV writer library for PHP.

v1.0.0(12mo ago)00MITPHPPHP &gt;=8.2

Since May 14Pushed 12mo ago1 watchersCompare

[ Source](https://github.com/jletrondo/csv-writer)[ Packagist](https://packagist.org/packages/jletrondo/csv-writer)[ RSS](/packages/jletrondo-csv-writer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

CsvWriter
=========

[](#csvwriter)

A simple and flexible CSV writer library for PHP.

Features
--------

[](#features)

- Write CSV files with custom delimiters, enclosures, and escape characters
- Supports writing to file or to string
- Easily set headers and write header rows
- Write rows as arrays or associative arrays
- Add rows from column-oriented arrays
- UTF-8 BOM support for Excel compatibility

Installation
------------

[](#installation)

To use the CsvReader library, include the file in your project and ensure that you have the necessary dependencies installed. You can install it via Composer:

```
composer require jletrondo/csv-writer
```

Usage
-----

[](#usage)

### Creating an Instance

[](#creating-an-instance)

To use the CsvReader library, include the following use statement at the top of your PHP file:

```
use Jletrondo\CsvReader\CsvReader;
```

You can create an instance of the CsvWriter class by passing optional parameters to the constructor.

```
    use Jletrondo\CsvWriter\CsvWriter;

    $writer = new CsvWriter('output.csv');
    $writer->setHeader(['Name', 'Email', 'Age']);
    $writer->writeHeader();
    $writer->writeRow(['John Doe', 'john@example.com', 30]);
    $writer->writeRow(['Jane Smith', 'jane@example.com', 25]);
    $writer->close();
```

### Write to String (no file)

[](#write-to-string-no-file)

```
    use Jletrondo\CsvWriter\CsvWriter;
    $writer = new CsvWriter(null); // No file path = write to string
    $writer->setHeader(['Product', 'Price']);
    $writer->writeHeader();
    $writer->writeRows([
        ['Apple', 1.2],
        ['Banana', 0.8],
    ]);
    $csvString = $writer->close();
    echo $csvString;
```

### Associative Arrays (header mapping)

[](#associative-arrays-header-mapping)

```
    use Jletrondo\CsvWriter\CsvWriter;

    $writer = new CsvWriter('users.csv');
    $writer->setHeader(['id', 'username', 'email']);
    $writer->writeHeader();
    $writer->writeRow(['id' => 1, 'username' => 'alice', 'email' => 'alice@example.com']);
    $writer->writeRow(['id' => 2, 'username' => 'bob', 'email' => 'bob@example.com']);
    $writer->close();
```

### Column-Oriented Data

[](#column-oriented-data)

```
    use Jletrondo\CsvWriter\CsvWriter;

    $writer = new CsvWriter('columns.csv');
    $writer->setHeader(['id', 'score']);
    $writer->writeHeader();
    $columns = [
        'id' => [1, 2, 3],
        'score' => [90, 85, 88],
    ];
    $writer->addRowsFromColumns($columns);
    $writer->close();
```

### Custom Delimiter, Enclosure, Escape

[](#custom-delimiter-enclosure-escape)

```
    use Jletrondo\CsvWriter\CsvWriter;

    $params = [
        'delimiter' => ';',
        'enclosure' => "'",
        'escape' => '\\',
        'has_header' => true,
    ];
    $writer = new CsvWriter('custom.csv', $params);
    $writer->setHeader(['a', 'b']);
    $writer->writeHeader();
    $writer->writeRow(['x', 'y']);
    $writer->close();
```

API
---

[](#api)

### Constructor

[](#constructor)

```
CsvWriter::construct(?string $output_path = null, array $params = [])
```

- `output_path`: File path to write to, or `null` for string output.
- `params`: Optional. Keys: `delimiter`, `enclosure`, `escape`, `has_header`.

### Methods

[](#methods)

- `setDelimiter(string $delimiter): void`
- `setEnclosure(string $enclosure): void`
- `setEscape(string $escape): void`
- `setHasHeader(bool $has_header): void`
- `setHeader(array $header): void`
- `writeHeader(): void`
- `writeRow(array $row): void`
- `writeRows(array $rows): void`
- `addRowsFromColumns(array $columns): void`
- `close(): ?string` — Finalize and close. Returns CSV string if writing to string.

License
-------

[](#license)

MIT

---

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance53

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

360d ago

### Community

Maintainers

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

---

Top Contributors

[![jletrondo](https://avatars.githubusercontent.com/u/61916708?v=4)](https://github.com/jletrondo "jletrondo (6 commits)")

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/jletrondo-csv-writer/health.svg)

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

###  Alternatives

[scheb/tombstone

Dead code detection with tombstones for PHP

282578.2k2](/packages/scheb-tombstone)

PHPackages © 2026

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