awesome-php-csv
AwesomePHPCSV is a simple, fast & lightweight CSV parser built for PHP 5.3+, (*1)
Breaking Changes in 2.1
1. hasHeadingRow parameter has been renamed to skipHeaderRow which is what it actually does, (*2)
2. mapColumns parameter has been added to use the values from the first row as associative array keys for the rest of the rows in the returned results (may lower performance), (*3)
Usage
AwesomePHPCSV is simple and easy to use. Just follow the instructions below:, (*4)
1. include AwesomePHPCSV into your own PHP project via Composer., (*5)
From the command line:, (*6)
composer require pfuri/awesomephp-csv:~2.1
Or in your composer.json file add pfuri/awesome-php-csv to the list of required packages and then run composer update:, (*7)
{
...
"require": {
...
"pfuri/awesome-php-csv": "~2.1",
...
}
...
}
2. create an AwesomePHPCSV instance:, (*8)
use pfuri\AwesomePHPCSV;
$apcsv = new AwesomePHPCSV();
Having trouble? Don't forget to include the Composer autoload.php file!", (*9)
require __DIR__ . '/vendor/autoload.php';
3. create an options array (see options section below for a full set of options):, (*10)
$options = array(
'pathToFile' => 'example.csv',
'hasHeaderRow' => true
);
4. call the import function the CSV into a PHP array:, (*11)
$data = $apcsv->import($options);
if($data === false) {
// false means there was some kind of error
// error messages can be found in the error message array
$errorMessages = $apcsv->errorMessages;
print_r($errorMessages);
}
The result is now a PHP array with each row representing a row from the CSV file (see the result format section below for an example), (*12)
Import Options
Below is the complete list of options for AwesomePHPCSV::import(array $options):, (*13)
-
string pathToFile (required): The full path to the csv file to be parsed (note: if file is used instead of pathToFile, then pathToFile is not required)
-
string file (optional): The CSV file in array format, where each row of the CSV is a string in the array (note: must not include line endings)
-
boolean skipHeaderRow (optional) (default: false): Whether or not to skip the first row
-
boolean mapColumns (optional) (default: false): use the values from the first row as associative array keys for the rest of the rows in the returned results (may lower performance)
-
int columns (optional) (default: null): Enables column validation by specifying how many the columns each row should have. If the csv contains a row without exactly this many columns, import will fail
-
int start (optional) (default:1): The row to start on [inclusive]
-
int end (optional) (default:null): The row to end on [inclusive]
-
int loopLimit (optional) (default:100): The amount of time to spend parsing a single row. You shouldn't need to change this
-
boolean debug (optional) (default:false): Prints error messages using debug() if any are encountered
If this is your CSV file:, (*14)
Year, Make, Model, Color, (*15)
2016, Cadillac, Escalade, Black, (*16)
2016, Mercedes Benz, ML350, Black, (*17)
Then after importing, you would receive a PHP array that looks like:, (*18)
[
[2016, Cadillac, Escalade, Black],
[2016, Mercedes Benz, ML350, Black]
]
Example File
example.php will import example.csv and output the results., (*19)