Coder Social home page Coder Social logo

csvtools's Introduction

CSV Tools

Simple PHP based classes for importing, parsing and structuring CSV files and data.

It is made up of just two files, which contain a parser and then a mapper. The parser class parses the CSV files, which the mapper class allows mapping of the CSV file headers to another set of keys. The mapper class provides allows iteration over the CSV data and array access to each of the columns. The mapper implements \Iterator, \ArrayAccess and \Countable.

Example usages:

filename.csv

NAME,MOBILE,MOBILE2,EMAIL,CITY,COUNTRY,BIRTH_DAY,WORK,NOTES,ACTIVE_ROLES,WANTED_ROLES
Jolly,2348030783839,,[email protected],NoCity,NoCountry,38758,My work,My Notes,,
Nolly,2348038983839,2348030783839,[email protected],NoCity,NoCountry,38758,My work,My Notes,,
,,,,,,,,,,
Polly,2348030783839,2348030783839,[email protected],NoCity,NoCountry,38758,My work,My Notes,,
Solly,2348030783457,2348030783839,[email protected],NoCity,NoCountry,38758,My work,My Notes,,
,,,,,,,,,,
,,,,,,,,,,
Lolly,2347090783839,,[email protected],NoCity,NoCountry,38758,My work,My Notes,,
Wolly,2347090783234,,[email protected],NoCity,NoCountry,38758,My work,My Notes,,
,,,,,,,,,,
$mapping = [
    'name' => 'NAME',
    'phone' => 'MOBILE',
    'phone2' => 'MOBILE2',
    'email' => 'EMAIL',
    'city' => 'CITY',
    'country' => 'COUNTRY',
    'dob' => 'BIRTH_DAY',
    'address' => 'WORK',
    'notes' => 'NOTES',
    'roles' => 'ACTIVE_ROLES',
    'additional_roles' => 'WANTED_ROLES',
];
$csv = (new CsvDataListMapper())
            ->setSourceFile('filename.csv')
            ->setDataMap($mapping);
foreach ($csv as $record) {
    write_to_database($record);
    print($record['name']);
}

prints

Jolly
Nolly

Polly
Solly


Lolly
Wolly

csvtools's People

Contributors

almaudoh avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

pnixmorph

csvtools's Issues

Empty rows should not be parsed

Right now, the CsvParser will include rows that have absolutely no content. There should be an option to skip empty records and not include them in the parsed result.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.