To convert an array into a CSV file we can use fputcsv() function. The fputcsv() function is used to format a line as CSV (comma separated values) file and writes it to an open file. The file which has to be read and the fields are sent as parameters to the fputcsv() function and it returns the length of the written string on success or FALSE on failure.
fputcsv( file, fields, separator, enclosure, escape )
After running the above PHP program you can find a file named
persons.csv in the same directory where the program file is located. When you open this file with a CSV file reader application like Microsoft Excel you’ll see the contents as shown in the below image.
Example 2: Suppose you want to put all the form inputs in a CSV file then first create a form to take input from the user and store it into an array. Then convert the array element into csv file.
- Input form:
- Input data display in excel as output:
- How to convert XML file into array in PHP?
- Convert multidimensional array to XML file in PHP
- Convert byte array to File using Java
- Convert Python Script to .exe File
- How to Convert Data URI to File then append to FormData?
- Convert Text file to JSON in Python
- Convert Text and Text File to PDF using Python
- PHP | $_FILES Array (HTTP File Upload variables)
- How to convert array to SimpleXML in PHP
- How to reset selected file with input tag file type in Angular 9?
- Python program to reverse the content of a file and store it in another file
- How to convert an array into object using stdClass() in PHP?
- Convert an object to associative array in PHP
- How to convert array values to lowercase in PHP ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.