1. Go to this page and download the library: Download palmtree/csv library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
palmtree / csv example snippets
$csv = new Reader('people.csv');
foreach($csv as $row) {
$name = $row['name'];
if (isset($row['age'])) {
echo "age is set!";
}
}
$csv = new Reader('/path/to/products.csv');
$csv->addNormalizers([
// Convert to integer
'product_id' => new NumberNormalizer(),
// Keep data as string but trim it
'name' => new StringNormalizer(),
// Convert to float, rounded to 4 decimal places
'price' => NumberNormalizer::create()->scale(4),
// Convert to boolean true or false
'enabled' => new BooleanNormalizer(),
// Convert to an array of integers
'related_product_ids' => new ArrayNormalizer(new NumberNormalizer()),
// Custom conversion with a callback
'specials' => new CallableNormalizer(fn ($value) => json_decode($value)),
]);
$csv = new Reader('people.csv', false);
// Alternatively, call the setHasHeaders() method after instantiation:
//$csv->setHasHeaders(false);
$csv = new Reader('people.csv');
// Headers are on the second row so let's set the offset to 1
$csv->setHeaderOffset(1);
$csv = new \Palmtree\Csv\InlineReader('"header_1","header_2"' . "\r\n" . '"foo","bar"');