How can the issue of fputcsv() treating the data as a string be resolved in the context of the code snippet?
Issue: The problem of fputcsv() treating the data as a string can be resolved by passing an array of data to the function instead of a string. This way, fputcsv() will correctly handle the data and write it to the CSV file with proper formatting. Code Snippet:
// Sample data
$data = array(
array('John Doe', 'john.doe@example.com', '25'),
array('Jane Smith', 'jane.smith@example.com', '30'),
);
// Open file pointer
$fp = fopen('data.csv', 'w');
// Iterate over data and write to CSV file
foreach ($data as $row) {
fputcsv($fp, $row);
}
// Close file pointer
fclose($fp);
Related questions
-
PHP multidimensional arrays sorting
-
header redirects PHP URL handling
-
arrays PHP pass by reference