Skip to content

Instantly share code, notes, and snippets.

@mrwadson
Last active August 10, 2023 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrwadson/103732d2004bd3496ef3e6722337b428 to your computer and use it in GitHub Desktop.
Save mrwadson/103732d2004bd3496ef3e6722337b428 to your computer and use it in GitHub Desktop.
CSV read and write
<?php
/*
* Usage [PHP >= 7.1]
*
require_once __DIR__ . '/Csv.php';
$csv = new CsvFile(__DIR__ . '/read_csv_file.csv');
$csvRows = $csv->getRows();
...
$csv->saveRows($csvRows, __DIR__ . '/save_csv_file.csv');
*/
class Csv
{
private string $file;
private array $cols;
public function __construct(string $file)
{
if (!file_exists($file)) {
throw new RuntimeException(sprintf('File %s does not exist', $file));
}
$this->file = $file;
}
public function getRows(int $length = 0, string $separator = ',', string $enclosure = '"'): array
{
$fp = fopen($this->file, 'rb');
$cols = $result = [];
$i = 0;
while ($row = fgetcsv($fp, $length, $separator, $enclosure)) {
if ($i === 0) {
foreach ($row as $col => $name) {
$this->cols[] = $name;
$cols[$col] = $this->convertColName($name);
}
} else {
foreach ($row as $col => $value) {
$result[$i - 1][$cols[$col]] = trim($value);
}
}
$i++;
}
fclose($fp);
return $result;
}
public function saveRows(array $data, string $file = null, bool $columns = true, string $separator = ',', string $enclosure = '"'): void
{
if (!$file) {
$file = $this->file;
}
$fp = fopen($file, 'wb');
if ($columns) {
fputcsv($fp, $this->cols, $separator, $enclosure);
}
foreach ($data as $row) {
fputcsv($fp, $row, $separator, $enclosure);
}
fclose($fp);
}
public function getDataMapping(int $asKey, array $asValue): array
{
$result = [];
$csvData = $this->getRows($this->file);
$keyName = $this->getKeyByIndex(reset($csvData), $asKey);
foreach ($csvData as $item) {
if (isset($item[$keyName])) {
$values = [];
foreach ($asValue as $val) {
$keyNameInValue = $this->getKeyByIndex(reset($csvData), $val);
if (isset($item[$keyNameInValue])) {
$values[] = $item[$keyNameInValue];
}
}
$result[$item[$keyName]] = count($values) > 1 ? $values : reset($values);
}
}
return $result;
}
private function getKeyByIndex(array $row, int $key): string
{
$keys = array_keys($row);
if (isset($keys[$key])) {
return $keys[$key];
}
throw new OutOfRangeException(sprintf('Out on range key #%d in array', $key));
}
private function convertColName(string $col): string
{
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '_', $col)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment