CSV Parser: Parse CSV data to indexed or associative arrays

Recommend this page to a friend!
  Info   View files Documentation   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 192 This week: 1All time: 8,414 This week: 243Up
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: 4073168 in United Kingdom
Week rank: 230 Up9 in United Kingdom Up

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 folder image Files  
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  /  src  
File Role Description
  Plain text file CSV_Parser.php Class Class source

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

 Version Control Unique User Downloads Download Rankings  
 100%
Total:192
This week:1
All time:8,414
This week:243Up

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