How can fgetcsv be used to read CSV files in PHP and convert them into arrays?

To read CSV files in PHP and convert them into arrays, you can use the fgetcsv function which reads a line from an open file and parses it as CSV fields. You can loop through the file line by line using fgetcsv and store the values in an array for each line. This way, you can read the CSV file and convert its contents into arrays for further processing.

$csvFile = fopen('example.csv', 'r'); // Open the CSV file for reading
$data = []; // Initialize an empty array to store the CSV data

while (($row = fgetcsv($csvFile)) !== false) {
    $data[] = $row; // Add each row as an array to the data array
}

fclose($csvFile); // Close the CSV file

print_r($data); // Output the CSV data as arrays