It doesn't matter for such small CSVs, but try to demand of those who give you CSVs to have them properly formatted, and not transposed or use online solutions to do so yourself. Here's the solution, but it stores the entire array in memory, even though the package in the first example is specifically made to avoid that.
With composer
You may use Spatie's simple-excel
like so:
$csv = __DIR__ . '/data.csv';
$data = [];
SimpleExcelReader::create($csv)
// ->useDelimiter(';') // Optional
->noHeaderRow() // Optional
->getRows()
->each(function(array $row) use (&$data) {
$length = count($row);
for ($i = 1; $i < $length; $i++) {
$data[$i - 1] ??= [];
$data[$i - 1][$row[0]] = $row[$i];
}
});
I also opened an issue for your use-case. (Issue was closed, due to the solution not being memory efficient)
Without composer
As said by "online Thomas", there's a native PHP function for that, and I find it easiest to use it, in general, like so:
$csv = __DIR__ . '/data.csv';
$data = array_map('str_getcsv', file($csv));
Caveat: does not produce the desired results, if fields contain linebreaks
Use closure in your case, or if you need a delimiter other than ',', etc.:
$csv = __DIR__ . '/data.csv';
$data = [];
array_map(function ($line) {
$row = str_getcsv($line);
$length = count($row);
for ($i = 1; $i < $length; $i++) use (&$data) {
$data[$i - 1] ??= [];
$data[$i - 1][$row[0]] = $row[$i];
}
}, file($csv));