CSV Parser: Parse CSV data to indexed or associative arrays

Recommend this page to a friend!
  Info   Documentation   View files (7)   Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 189 All time: 8,409 This week: 327
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: 4063170 in United Kingdom
Week rank: 713 24 in United Kingdom

Details

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  
File Role Description
src (1 file)
test (1 file)
.travis.yml Data Auxiliary data
autoload.php Aux. Configuration script
composer.json Data Auxiliary data
phpunit.xml Data Auxiliary data
README.md Doc. Auxiliary data

  Files  /  src  
File Role Description
   CSV_Parser.php Class Class source

  Files  /  test  
File Role Description
   CSV_ParserTest.php Test Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:189
This week:0
All time:8,409
This week:327

For more information send a message to info at phpclasses dot org.