Код: Выделить всё
class Csv {
public $src;
public $trgt;
function baseMethod(callable $callback) {
$rownum = 0;
$header = [];
if (($fhs = fopen($this->src, 'r+')) &&
($fht = fopen($this->trgt, 'w+'))) {
while (($csvr = fgetcsv($fhs, null, ';', '"')) !== false) {
if ($rownum++ == 1) {
$header = $csvr;
fputcsv($fht, $header);
} else {
$row = array_combine($header, $csvr);
$row = $callback($row, $header); # only this part changes, and sometimes the header...
fputcsv($fht, $row);
}
}
fclose($fhs);
fclose($fht);
}
}
function sortColums() {
$this->baseMethod(function($row, $header) {
foreach ($this->sortCols as $col) {
if (!empty($row[$col]) && str_contains($row[$col], '|')) {
$colValues = explode('|', $row[$col]);
sort($colValues);
$row[$col] = implode('|', $colValues);
}
}
return $row;
});
}
}
Подробнее здесь: https://stackoverflow.com/questions/789 ... oop-in-php
Мобильная версия