What are the csv functions in PHP and how can they be used to manipulate data?
The csv functions in PHP allow you to easily read from and write to CSV files, making it simple to manipulate data stored in this format. These functions include fgetcsv() for reading a line from a CSV file, fputcsv() for writing an array to a CSV file, and str_getcsv() for parsing a CSV string into an array. By using these functions, you can efficiently work with CSV data in your PHP applications.
// Example of reading data from a CSV file
$csvFile = fopen('data.csv', 'r');
while (($data = fgetcsv($csvFile)) !== false) {
// Process each row of data
print_r($data);
}
fclose($csvFile);
// Example of writing data to a CSV file
$csvFile = fopen('data.csv', 'w');
$data = array('John', 'Doe', 'john.doe@example.com');
fputcsv($csvFile, $data);
fclose($csvFile);
// Example of parsing a CSV string into an array
$csvString = "John,Doe,john.doe@example.com";
$data = str_getcsv($csvString);
print_r($data);