What potential issues can arise when using fputcsv() in PHP to export data to a CSV file?
One potential issue when using fputcsv() in PHP to export data to a CSV file is that special characters, such as commas or double quotes, can interfere with the CSV formatting. To solve this issue, you can use the PHP function mb_convert_encoding() to convert the data to UTF-8 before writing it to the CSV file.
// Sample data array
$data = array(
array('Name', 'Age', 'Country'),
array('John Doe', 30, 'USA'),
array('Jane Smith', 25, 'Canada'),
);
// Open a file for writing
$file = fopen('data.csv', 'w');
// Convert data to UTF-8 before writing to CSV
foreach ($data as $row) {
fputcsv($file, array_map('utf8_encode', $row));
}
// Close the file
fclose($file);