How can the fgetcsv function be utilized to read only certain columns from a CSV file in PHP?
To read only certain columns from a CSV file using the fgetcsv function in PHP, you can read the entire row and then access only the desired columns by their index. You can then store these values in an array or process them as needed.
$csvFile = fopen('data.csv', 'r');
while (($row = fgetcsv($csvFile)) !== false) {
$column1 = $row[0]; // Accessing the first column
$column3 = $row[2]; // Accessing the third column
// Process or store the values as needed
}
fclose($csvFile);