'Is it possible to read a csv file column by column with League csv?
I wondered if it was possible to read a csv file written like this in symfony using League csv or something else.
water_level,2,456,345
wind_speed,25,456,56
food_level,10.4,123,23
animal_count,56,34,124
number_of_machines,150,345,54
machineId,1234567,1234568,1234567
Header in the first column and data in the next columns.
Right now, I can read the files when the header is in the first row, I just want to know if it's possible to read it the other way!
I tried transposing but I get an error. I don't really know what's causing it if the array really is transposed.
$pathString = implode($newFilesPath);
$reader = (Reader::createFromPath($pathString))->setHeaderOffset(0);
$results = $this->transpose(iterator_to_array($reader->getRecords()));
foreach ($results as $row) {
$properties = (new AppProperty)
->setWaterLevel($row['water_level'])
->setWindSpeed($row['wind_speed'])
->setFoodLevel($row['food_level'])
->setAnimalCount($row['animal_count'])
->setNumberOfMachines($row['number_of_machines'])
->setMachineId($row['machineId'])
->setDate(new \DateTime());
$this->em->persist($properties);
}
$this->em->flush();
}
private function transpose ($array){
return array_map(null, $array);
}
The error:
Argument 1 passed to App\Entity\Property::setWaterLevel() must be of the type int or null, string given.
It looks like the transpose just didn't work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|