PHPackages                             jd/csv-objects - 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. jd/csv-objects

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

jd/csv-objects
==============

Library to import CSV or XLSX files into an array of objects according to an specified definition

07742[2 PRs](https://github.com/j-d/csv-objects/pulls)PHPCI failing

Since Sep 30Pushed 1y ago1 watchersCompare

[ Source](https://github.com/j-d/csv-objects)[ Packagist](https://packagist.org/packages/jd/csv-objects)[ RSS](/packages/jd-csv-objects/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (2)Used By (0)

[![Build status](https://camo.githubusercontent.com/dcbd47820837a132bd2db3fea1791b6b59663104385053cdf7ed46c0940e69af/68747470733a2f2f636972636c6563692e636f6d2f67682f6a2d642f6373762d6f626a656374732e7376673f7374796c653d736869656c6426636972636c652d746f6b656e3d3a636972636c652d746f6b656e)](https://circleci.com/gh/j-d/csv-objects)

CSV to Objects
==============

[](#csv-to-objects)

Library to import CSV or XLSX files into an array of objects according to an specified definition

Instructions
------------

[](#instructions)

To convert a file into an an array of objects, you must provide an `ImportDefinition`. This object is created by passing an array in an specific format. We recommend using YAML for defining this file as it is human-friendly. This format can be quite sophisticated, so two examples are provided:

### Basic import definition

[](#basic-import-definition)

```
name: Fruits definition                                  # (Optional) Name of the import definition
columns:                                                 # (Required) An associative array with the headings of the columns in the file that will be imported
    Name:   { fruit: '#Name#' }                          # 'fruit' is a shortname for the object being created for the list (defined below) and #Name# will be the argument passed to the constructor. The hashes indicate that it should replace it with the value on that column
    Weight: ~                                            # Null indicates that it can be ignored
classes:                                                 # (Required) An associative array with the mapped classes
    fruit: 'CSVObjects\ImportBundle\Tests\Objects\Fruit' # The first one will be the class that will be returned
```

### Full import definition

[](#full-import-definition)

All the extra properties are optional.

```
name: Fruits definition
columns:
    File Version:  { expect: 38 }                                                                                             # Expects indicates that the row value must always be as stated
    Name:          { fruit: ['#Name#', '#Colour#', '#Origin#', '#Origin - City#', '#Class#', '#Expiry date#', '#Contract#'] } # The constructor now takes an array of arguments
    Weight:        ~
    Colour:        { validate: ['red', 'yellow'] }                                                                            # Valid values that this column could have
    Origin:        { map: { 'Granada': 'Spain', 'Malaga': 'Spain', 'Dover': 'UK' } }                                          # It will apply this alias to the data on this column. If no validate is provided, non-matching values will convert into null
    Origin - City: ~
    Class:         { extract: 'Class (.*)', validate: ['A+', 'A', 'B', 'C'] }                                                 # It is like mapping but using the expecified regular expression to do the conversion. The desired part must be in brackets. Functions can optionally be combined when required
    Expiry date:   { validate: 'date', sourceFormat: 'd/m/Y', format: 'Y-m-d' }                                               # If validate is not an array, it can be an special value. 'date' will check that it is a date. Optionally, you can specify the source format using 'sourceFormat' or reformat it by specifying 'format'
    Contract:      { contract: ['#Contract#'] }
copy:                                                                                                                         # If specified, if will add more columns to the row, copying from the referenced columns before being processed
    Origin - City: Origin
classes:
    fruit:    ['CSVObjects\ImportBundle\Tests\Objects\Fruit', 'getFruitFromFullInfo']                                         # If the mapped class is an array, it will make a static call to the specified method to create the class
    contract: ['CSVObjects\ImportBundle\Tests\Objects\Contract', 'getContractFromId' ]
```

For the object mappings, you can also use a service in the `['@service_name', 'method']` notation.

For a more complex example see `school-results.yml` and `school-results.csv`.

###  Health Score

22

—

LowBetter than 21% of packages

Maintenance28

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity27

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2644?v=4)[Julien Danjou](/maintainers/jd)[@jd](https://github.com/jd)

---

Top Contributors

[![aleherse](https://avatars.githubusercontent.com/u/2232890?v=4)](https://github.com/aleherse "aleherse (10 commits)")[![j-d](https://avatars.githubusercontent.com/u/1140726?v=4)](https://github.com/j-d "j-d (9 commits)")[![pedropena](https://avatars.githubusercontent.com/u/834583?v=4)](https://github.com/pedropena "pedropena (3 commits)")[![victormilla](https://avatars.githubusercontent.com/u/53478954?v=4)](https://github.com/victormilla "victormilla (1 commits)")

### Embed Badge

![Health badge](/badges/jd-csv-objects/health.svg)

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

###  Alternatives

[kbs1/laravel-abbreviations

Abbreviations / acronyms support for your app.

1018.9k](/packages/kbs1-laravel-abbreviations)[opanegro/nova-custom-controller

Make custom controller in Laravel Nova

144.4k](/packages/opanegro-nova-custom-controller)

PHPackages © 2026

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