agentsib / csv-reader
Simple way read CSV file
Installs: 55 358
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: >=5.4
This package is auto-updated.
Last update: 2024-10-21 10:42:53 UTC
README
Simple way read CSV file
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 = new AgentSIB\CsvReader\CsvReader($resource, true, ';'); // Get headers array $csv->getHeaders(); // ["id", "name", "value"] // Check header $csv->hasHeader('id'); // true foreach ($csv as $row) { // Get value by column number echo $row[1]; // test1,test2 // Get value by column name echo $row['value']; // value1,value2 // Get value by not exists column echo $row['not_exists']; // null // Check value for exists isset($row[1]); // true isset($row[20]); // false isset($row['value']); //true isset($row['not_exists']); //false } $csv->rewind(); $firstRow = $csv->current(); foreach ($firstRow as $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 ($firstRow as $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 ($firstRow as $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 = new AgentSIB\CsvReader\CsvReader($resource, false, ';'); // Get headers array $csv->getHeaders(); // [] foreach ($csv as $row) { // Get value by column number echo $row[1]; // test1,test2 echo $row[20]; // null,null // Check value for exists isset($row[1]); // true isset($row[20]); // false } $csv->rewind(); $firstRow = $csv->current(); foreach ($firstRow as $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 ($firstRow as $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 ($firstRow as $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 = new AgentSIB\CsvReader\CsvReader($resource, ['id', 'name', 'value'], ';'); // Get headers array $csv->getHeaders(); // ['id', 'name', 'value'] $csv->rewind(); $firstRow = $csv->current(); foreach ($firstRow as $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 = new AgentSIB\CsvReader\CsvReader($r);