Skip to content

Latest commit

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

CSV Reader

Simple way read CSV file

Latest Stable VersionTotal DownloadsLatest Unstable VersionLicense

Installation

Install via composer:

composer require agentsib/csv-reader

Read CSV file with headers

Example file test.csv:

id;name;value
1;test1;value1
2;test2;value2

Read file:

<?$resource = fopen('test.csv','r');
$csv = newAgentSIB\CsvReader\CsvReader($resource, true, ';');
// Get headers array$csv->getHeaders(); // ["id", "name", "value"]// Check header $csv->hasHeader('id'); // trueforeach ($csvas$row) {
// Get value by column numberecho$row[1]; // test1,test2// Get value by column nameecho$row['value']; // value1,value2// Get value by not exists columnecho$row['not_exists']; // null// Check value for existsisset($row[1]); // trueisset($row[20]); // falseisset($row['value']); //trueisset($row['not_exists']); //false 
}
$csv->rewind();
$firstRow = $csv->current();
foreach ($firstRowas$key => $value) {
echo$key.' => '.$value.PHP_EOL;
}
// id => 1// name => test1// value => value1// Replace headers example$csv->replaceHeaders(['prop_id', 'prop_name', 'prop_value']);
$csv->getHeaders(); // ['prop_id', 'prop_name', 'prop_value']$csv->rewind();
$firstRow = $csv->current();
foreach ($firstRowas$key => $value) {
echo$key.' => '.$value.PHP_EOL;
}
// prop_id => 1// prop_name => test1// prop_value => value1// Replace headers example 2$csv->replaceHeaders(['prop_id', 'prop_name']);
$csv->getHeaders(); // ['prop_id', 'prop_name']$csv->rewind();
$firstRow = $csv->current();
foreach ($firstRowas$key => $value) {
echo$key.' => '.$value.PHP_EOL;
}
// prop_id => 1// prop_name => test1

Read CSV file without headers

Example file test.csv:

1;test1;value1
2;test2;value2

Read file:

<?$resource = fopen('test.csv','r');
$csv = newAgentSIB\CsvReader\CsvReader($resource, false, ';');
// Get headers array$csv->getHeaders(); // []foreach ($csvas$row) {
// Get value by column numberecho$row[1]; // test1,test2echo$row[20]; // null,null// Check value for existsisset($row[1]); // trueisset($row[20]); // false
}
$csv->rewind();
$firstRow = $csv->current();
foreach ($firstRowas$key => $value) {
echo$key.' => '.$value.PHP_EOL;
}
// 0 => 1// 1 => test1// 2 => value1// Replace headers example$csv->replaceHeaders(['prop_id', 'prop_name', 'prop_value']);
$csv->getHeaders(); // ['prop_id', 'prop_name', 'prop_value']$csv->rewind();
$firstRow = $csv->current();
foreach ($firstRowas$key => $value) {
echo$key.' => '.$value.PHP_EOL;
}
// prop_id => 1// prop_name => test1// prop_value => value1// Replace headers example 2$csv->replaceHeaders(['prop_id', 'prop_name']);
$csv->getHeaders(); // ['prop_id', 'prop_name']$csv->rewind();
$firstRow = $csv->current();
foreach ($firstRowas$key => $value) {
echo$key.' => '.$value.PHP_EOL;
}
// prop_id => 1// prop_name => test1

You can set headers on initialize:

<?php$resource = fopen('test.csv','r');
$csv = newAgentSIB\CsvReader\CsvReader($resource, ['id', 'name', 'value'], ';');
// Get headers array$csv->getHeaders(); // ['id', 'name', 'value']$csv->rewind();
$firstRow = $csv->current();
foreach ($firstRowas$key => $value) {
echo$key.' => '.$value.PHP_EOL;
}
// id => 1// name => test1// value => value1

Known issue

If you use stream php://input, you need save content to another stream, because rewind function not work in this case. For example:

<?php$resource = fopen('php://input', 'rb');
$r = fopen('php://memory', 'r+');
fwrite($r, stream_get_contents($resource));
rewind($r);
$csv = newAgentSIB\CsvReader\CsvReader($r);

About

Simple CSV reader for PHP

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages