PHPackages                             keboola/csvmap - 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. keboola/csvmap

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

keboola/csvmap
==============

Flatten an object into a CSV file(s)

2.3.0(1y ago)35.0k↓46.7%1[1 issues](https://github.com/keboola/php-csvmap/issues)2MITPHPPHP ^8.1

Since Mar 30Pushed 1y ago15 watchersCompare

[ Source](https://github.com/keboola/php-csvmap)[ Packagist](https://packagist.org/packages/keboola/csvmap)[ RSS](/packages/keboola-csvmap/feed)WikiDiscussions master Synced today

READMEChangelog (9)Dependencies (7)Versions (37)Used By (2)

CsvMap
======

[](#csvmap)

[![Build Status](https://camo.githubusercontent.com/3ee3cd4a71d72177d807eec04896d45374d695cdb1568ec5c9279cbab82e5908/68747470733a2f2f7472617669732d63692e6f72672f6b65626f6f6c612f7068702d6373766d61702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/keboola/php-csvmap)[![Latest Stable Version](https://camo.githubusercontent.com/a976de1a045c0c370ed743c01b0feaf4c16a3bd6e48f2e5635e301500f8fb2d4/68747470733a2f2f706f7365722e707567782e6f72672f6b65626f6f6c612f6373766d61702f76657273696f6e)](https://packagist.org/packages/keboola/csvmap)[![Total Downloads](https://camo.githubusercontent.com/5d2aa7fdf326b344ca478ec89230cb98fe61c68ea882533e5c01b2594da8f464/68747470733a2f2f706f7365722e707567782e6f72672f6b65626f6f6c612f6373766d61702f646f776e6c6f616473)](https://packagist.org/packages/keboola/csvmap)[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](https://github.com/keboola/php-csvmap/blob/master/LICENSE.md)

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

[](#installation)

```
composer require keboola/csvmap
```

Usage
-----

[](#usage)

For example, map key `key.nested` of each `$data` array item, to CSV column `mappedKey`.

```
$data = [
    [
        'id => '123',
        'key' => [
            'nested' => 'value1'
        ]
    ]
];
$mapping = [
    'id' => [
        'type' => 'column',
        'mapping' => [
            'destination' => 'id',
            'primaryKey' => true,
        ]
    ],
    'key.nested' => 'mappedKey'
];

$rootType = 'rootName';
$userData = [];
$parser = new Mapper($mapping, $writeHeader, $rootType);
$parser->parse($data, $userData);

$files = $parser->getCsvFiles();
$tempFilePath = $files['rootName']->getPathName();

```

Mapping
-------

[](#mapping)

- The mapping is an array.
- The **`key`** corresponds to the one root/nested key in the source data. Default delimiter is `.`, eg. `key`, `key.nested`.
- The **`value`** is the mapping configuration for the given key. It is `string` (shorthand notation) or `array`.
    - There are 3 types of the mapping, defined by the `type` key:
    - `type` (optional), `column` by default.
        - `column` will store the value from its key into a CSV column
        - `user` will look into an array in the second argument of the parse function and fill a CSV column with its value
        - `table` will create a "child" CSV and link through a primary key or a hash, if no primary key is defined

Column mapping
--------------

[](#column-mapping)

- `mapping`: Required, must contain `destination`:
    - `destination`: Target column in the output CSV file
    - `primaryKey`: Optional, boolean. If set to true, the column will be included in the primary key
- `forceType`: Optional, if a value is not scalar, it'll be JSON encoded

#### Shorthand notation

[](#shorthand-notation)

- If the **`value`** is the `string`, then it is a shorthand notation for the `column` mapping.
- The string value corresponds to `mapping.destination`.

Example shorthand notation:

```
[
     'key.nested' => 'mappedKey'
]

```

... is equal to

```
[
    'key.nested' => [
        'type' => 'column',
        'mapping' => [
            'destination' => 'mappedKey'
        ]
    ]
]

```

#### Examples

[](#examples)

Four different `column` mappings.

```
[
    'id' => [
        'type' => 'column',
        'mapping' => [
            'destination' => 'id',
            'primaryKey' => true,
        ]
    ],
    'name' => 'name',
    'info.url' => 'url,
    'info.tags' => [
        'type' => 'column',
        'forceType' => true,
        'mapping' => [
            'destination' => 'tags'
        ]
    ]
]

```

User mapping
------------

[](#user-mapping)

Same as `column`, except the **key** of the object is not searched for in the parsed data, but in an array passed to the parser to inject user data

Table mapping
-------------

[](#table-mapping)

- `destination`: Required, a target CSV file name
- `tableMapping`: Required, mapping of all child table's columns
    - Sub-mapping has the same structure as the root `$mapping`.
- `parentKey`: Optional, can be used to set the parent/child link as a primary key in the child or override the link's column name in the child
    - `primaryKey`: boolean, same as in `column`
    - `destination`: Name of the link column (if not used, name of the parent table . `_pk` is used by default)
    - `disable`: boolean, if set to non-false value, the parent key in the child table, as well as the column in the parent will not be saved

*Note:*
If the `destination` is the same as the current parsed 'type' (destination of the parent),
`parentKey.disable` **must** be true to preserve consistency of structure of the child and parent.

#### Map scalar items to a separated CSV

[](#map-scalar-items-to-a-separated-csv)

- Table mapping is useful when you need to map array of the **objects** to separate CSV tables.
- But sometimes you need to map an array of the **scalar** (not object) values, for example a list of tags.
- In this case, you can use an **empty key** in `tableMapping` to map a scalar value.

For example, we have this data:

```
[
    ['id' => 1, 'name' => 'dog', 'tags' => ['useful', 'pet', 'animal']],
    ['id' => 2, 'name' => 'mouse', 'tags' => ['harmful', 'animal']]
]

```

Example mapping:

```
[
    'id' => [
        'type' => 'column',
        'mapping' => [
            'destination' => 'id',
            'primaryKey' => true,
        ]
    ],
    'name' => 'name',
    'tags' => [
        'type' => 'table',
        'destination' => 'tags',
        'tableMapping' => [
            '' => 'tagName' // empty key used to map scalar value
        ]
    ]
]

```

Results:

`root.csv`:

```
"id","name"
"1","dog"
"2","mouse"
```

`tags.csv`:

```
"tagName","root_pk"
"useful","1"
"pet","1"
"animal","1"
"harmful","2"
"animal","2"
```

#### Examples

[](#examples-1)

Mixed `column` and `table` mappings.

```
[
    'id' => [
        'type' => 'column',
        'mapping' => [
            'destination' => 'id',
            'primaryKey' => true,
        ]
    ],
    'name' => "name,
    'addresses' => [
        'type' => 'table',
        'destination' => 'addresses',
        'tableMapping' => [
            'number' => 'number',
            'street' => [
                'type' => 'table',
                'destination' => 'streets',
                'tableMapping' => [
                    'name' => 'name'
                ]
            ]
         ]
    ]
]

```

License
-------

[](#license)

MIT licensed, see [LICENSE](./LICENSE) file.

###  Health Score

44

—

FairBetter than 90% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community22

Small or concentrated contributor base

Maturity84

Battle-tested with a long release history

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~112 days

Recently: every ~371 days

Total

29

Last Release

588d ago

Major Versions

0.6.0 → 1.0.02020-10-27

1.1.0 → 2.0.02022-04-27

PHP version history (3 changes)0.0.1PHP &gt;=5.6.0

1.0.0PHP ^7.1

2.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/101dbf2551a0709ddab522f97669f13a2c4cc2d0a1e8d009f3af6ba80accb1a9?d=identicon)[Keboola](/maintainers/Keboola)

---

Top Contributors

[![ujovlado](https://avatars.githubusercontent.com/u/419849?v=4)](https://github.com/ujovlado "ujovlado (24 commits)")[![AdamVyborny](https://avatars.githubusercontent.com/u/27994036?v=4)](https://github.com/AdamVyborny "AdamVyborny (14 commits)")[![michaljurecko](https://avatars.githubusercontent.com/u/19371734?v=4)](https://github.com/michaljurecko "michaljurecko (9 commits)")[![kachnitel](https://avatars.githubusercontent.com/u/4067705?v=4)](https://github.com/kachnitel "kachnitel (9 commits)")[![themark147](https://avatars.githubusercontent.com/u/17779619?v=4)](https://github.com/themark147 "themark147 (3 commits)")[![MiroCillik](https://avatars.githubusercontent.com/u/1488015?v=4)](https://github.com/MiroCillik "MiroCillik (2 commits)")[![Halama](https://avatars.githubusercontent.com/u/903531?v=4)](https://github.com/Halama "Halama (2 commits)")

---

Tags

csvobjectflatten

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/keboola-csvmap/health.svg)

```
[![Health](https://phpackages.com/badges/keboola-csvmap/health.svg)](https://phpackages.com/packages/keboola-csvmap)
```

###  Alternatives

[maatwebsite/excel

Supercharged Excel exports and imports in Laravel

12.9k157.3M892](/packages/maatwebsite-excel)[league/csv

CSV data manipulation made easy in PHP

3.5k182.1M860](/packages/league-csv)[rap2hpoutre/fast-excel

Fast Excel import/export for Laravel

2.3k27.0M52](/packages/rap2hpoutre-fast-excel)[openspout/openspout

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

1.2k70.2M242](/packages/openspout-openspout)[goodby/csv

CSV import/export library

9865.7M25](/packages/goodby-csv)[sonata-project/exporter

Lightweight Exporter library

44921.6M40](/packages/sonata-project-exporter)

PHPackages © 2026

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