PHP Classes
elePHPant
Icontem

PG CSV: Parse and write data from arrays into CSV files

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Info   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings  
2014-10-27 (10 months ago) RSS 2.0 feedNot enough user ratingsTotal: 206 All time: 7,476 This week: 1,049Up
Version License PHP version Categories  
pg-csv 0.1MIT/X Consortium ...5PHP 5, Files and Folders
Description Author  

This class can parse and write data from arrays into CSV files.

It can read and parse a CSV file and return its data as an array.

The class can write records of data from associative arrays to a CSV file.

Picture of P Guardiario
Name: P Guardiario <contact>
Classes: 6 packages by
Country: Philippines Philippines
Age: ???
All time rank: 171618 in Philippines Philippines
Week rank: 551 Up8 in Philippines Philippines Up

Details provided by the author  
pgcsv
=====

A 'pretty good' CSV library for Php


### Writing
-----------

```php
$fields = array('name', 'phone', 'address');

$csv = new CSV('output.csv', $fields);

$person = array(
  'name' => 'Jow Brown',
  'phone' => '123 222-0123',
  'address' => '666 Buckingham Palace'
);

$csv->save($person);
$csv->close();
```

By default, CSV saves data with a utf-8 BOM to make it Excel-friendly. You can change that though.

### Reading
-----------

```php
$data = CSV::read('input.csv');
```

CSV expects the first row to be headers, but you can change that. You can also change the delimiter.

### Iterating
-----------

```php
foreach(CSV::iterate('input.csv') as $row){
  echo $row['name'] . "\n";
}
```

Iterating this way means the whole file does not need to be in memory.

### CSV Generation
-----------

```php
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");

$fields = array('name', 'phone', 'address');

$csv = new CSV('php://output', $fields);

$person = array(
  'name' => 'Jow Brown',
  'phone' => '123 222-0123',
  'address' => '666 Buckingham Palace'
);

$csv->save($person);
$csv->close();
```
  Files folder image Files  
File Role Description
Plain text file pgcsv.php Class Class source
Accessible without login Plain text file README.md Doc. Documentation
Plain text file test.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%Total:206All time:7,476
 This week:0This week:1,049Up