CSV to array

function csvToArray(string $filename): array
{
    $result = [];
    $header = [];
    $isFirstLine = true;
    if (($handle = fopen($filename, "r")) !== false) {
        while (($data = fgetcsv($handle, 1000, ",")) !== false) {
            if ($isFirstLine) {
                $header = $data;
                $isFirstLine = false;
                continue;
            }

            $row = [];
            foreach ($data as $c => $value) {
                $row[$header[$c]] = $value;
            }
            $result[] = $row;
        }
        fclose($handle);
    }
    return $result;
}

Array to CSV

function arrayToCsv(string $filename, array $lines): void
{
    $firstRecord = $lines[0];
    $header = array_keys($firstRecord);
    if (($handle = fopen($filename, "w")) !== false) {
        fputcsv($handle, $header);
        foreach ($lines as $line) {
            fputcsv($handle, $line);
        }
        fclose($handle);
    }
}