How can fgetcsv() be used to read a CSV file in PHP and store the data in an array?
To read a CSV file in PHP and store the data in an array, you can use the fgetcsv() function. This function reads a line from an open file and parses it as CSV fields. You can loop through the file line by line, using fgetcsv() to parse each line and store the data in an array.
$file = fopen('data.csv', 'r');
$data = [];
while (($line = fgetcsv($file)) !== false) {
$data[] = $line;
}
fclose($file);
print_r($data);
Related Questions
- What common mistakes do beginners make when including HTML forms in PHP files for data submission?
- What are the advantages and disadvantages of using the LIKE operator in a SQL query compared to an exact match comparison in PHP?
- When working with large text files in PHP, what are some best practices for efficiently sorting and processing the data?