PHP Classes

CSV Parser: Parse CSV data to indexed or associative arrays

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 208 All time: 8,392 This week: 70Up
Version License PHP version Categories
csv-to-arrays 2.0.1MIT/X Consortium ...5.3.0PHP 5, Files and Folders, Parsers
Description 

Author

This class can parse CSV data to indexed or associative arrays.

It can read CSV data from file, an input stream or a string to parse and extract the record elements.

Each row can be parsed to an associative array using the header names as keys or a regular array with numbered indexes.

Picture of Jabran Rafique
Name: Jabran Rafique <contact>
Classes: 1 package by
Country: United Kingdom
Age: ???
All time rank: 4117181 in United Kingdom
Week rank: 216 Up13 in United Kingdom Up

Documentation

CSV Parser (PHP)

PHP client to parse CSV data from a file, stream or string into indexed or associative arrays.

Install

Install using composer

#composer.json

{
  "require": {
    "jabranr/csv-parser": ">=1.0.0"
  }
}

Run following to install

$ comsposer install

Use

Initiate a new instance

$csv = new CSV_Parser();

API

Get data from a string

/@param: string $str/
$csv->fromString( $str );

Get data from a stream

/@param: stream $stream (e.g. php://input)/
$csv->fromStream( $stream );

Get data from a file

/@param: file $file/
$csv->fromFile( $file );

Parse data for output

/ 
 * Set $headers true/false to include top/first row 
 * and output an associative array
 *
 * @param: boolean $headers (Default: true)
 * @return: array
 */
$csv->parse( $headers );

Example

Example input string

$str = 'id,first_name,last_name;1,Jabran,Rafique';

$csv->fromString( $str );

// Output with headers:
$csv->parse();

Array(
  [id] => 1,
  [first_name] => 'Jabran',
  [last_name] => 'Rafique'
)

// Output without headers:
$csv->parse( false );

Array(
  [0] => array(
    [0] => 'id',
    [1] => 'first_name',
    [2] => 'last_name'
  ),
  [1] => array(
    [0] => 1,
    [1] => 'Jabran',
    [2] => 'Rafique'
  )
)

License

&copy; 2015 MIT License - Jabran Rafique

Analytics


  Files folder image Files (7)  
File Role Description
Files folder imagesrc (1 file)
Files folder imagetest (1 file)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file autoload.php Aux. Configuration script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Auxiliary data

  Files folder image Files (7)  /  src  
File Role Description
  Plain text file CSV_Parser.php Class Class source

  Files folder image Files (7)  /  test  
File Role Description
  Accessible without login Plain text file CSV_ParserTest.php Test Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:208
This week:0
All time:8,392
This week:70Up