How to convert an array to CSV file in PHP ? Last Updated : 01 Aug, 2021 Comments Improve Suggest changes Like Article Like Report 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. Syntax : fputcsv( file, fields, separator, enclosure, escape ) Example 1: php <?php // Create an array of elements $list = array( ['Name', 'age', 'Gender'], ['Bob', 20, 'Male'], ['John', 25, 'Male'], ['Jessica', 30, 'Female'] ); // Open a file in write mode ('w') $fp = fopen('persons.csv', 'w'); // Loop through file pointer and a line foreach ($list as $fields) { fputcsv($fp, $fields); } fclose($fp); ?> 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. Output in Microsoft Excel 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. index.html <!DOCTYPE html> <html> <head> <title> How to Convert Array to CSV file in PHP ? </title> </head> <body> <!-- form tag to create form --> <form action = "gfg.php" method = "post"> Name <input type = "text" name = "name" /> <br><br> Email <input type = "text" name = "email" /> <br><br> Phone <input type = "text" name = "phone" /> <input type = "submit" name = "submit" value = "Submit"> </form> </body> </html> gfg.php <?php $data = array( $_POST['name'], $_POST['email'], $_POST['phone'] ); // Open file in append mode $fp = fopen('databse.csv', 'a'); // Append input data to the file fputcsv($fp, $data); // close the file fclose($fp); ?> Output: Input form: Input data display in excel as output: PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples. Comment More infoAdvertise with us Next Article How to convert an array to CSV file in PHP ? frikishaan Follow Improve Article Tags : Technical Scripter Web Technologies PHP CSV PHP-Misc +1 More Similar Reads How to Convert JSON file into CSV in PHP ? In this article, we are going to see how to convert JSON data into a CSV file using PHP. JSON (JavaScript Object Notation) is a dictionary-like notation that can be used to structuring data. It is stored with the extension .json, for example - geeksforgeeks.json On the other hand, CSV (or Comma Sepa 2 min read How to Append Data to a File in PHP? Appending data to a file is a common operation in PHP when you want to add new information without overwriting the existing content. This can be particularly useful for logging, saving user activity, or adding records incrementally. We will explore different approaches to appending data to a file in 2 min read How to parse a CSV File in PHP ? In this article, we learn to parse a CSV file using PHP code, along with understanding its basic implementation through examples. Â Approach: The following approach will be utilized to parse the CSV File using PHP, which is described below: Step 1. Add data to an Excel file. The following example is 3 min read How to Convert Number to Character Array in PHP ? Given a number, the task is to convert numbers to character arrays in PHP. It is a common operation when you need to manipulate or access individual digits of a number. This can be particularly useful in situations where you need to perform operations on the digits of a number, such as digital root 3 min read How to Convert CSV to JSON file and vice-versa in JavaScript ? CSV files are a common file format used to store data in a table-like manner. They can be particularly useful when a user intends to download structured information in a way that they can easily open and read on their local machine. CSV files are ideal for this because of their portability and unive 3 min read How to Convert a Given Number to Words in PHP? Given an integer N, your task is to convert the given number into words using PHP.Examples:Input: N = 958237764Output: Nine Hundred Fifty Eight Million Two Hundred Thirty Seven Thousand Seven Hundred Sixty FourInput: N = 5000Output: Five ThousandBelow are the approaches to convert a given number to 5 min read Convert multidimensional array to XML file in PHP Given a multi-dimensional array and the task is to convert this array into an XML file. To converting the multi-dimensional array into an xml file, create an XML file and use appendChild() and createElement() function to add array element into XML file. Example: First, create a PHP Multidimensional 2 min read How to Slice an Array in PHP? In PHP, slicing an array means taking a subset of the array and extracting it according to designated indices. When you need to extract a subset of elements from an array without changing the original array, this operation comes in handy. PHP comes with a built-in function called array_slice() to he 2 min read How to use array_merge() and array_combine() in PHP ? In this article, we will discuss about how to use array_merge() and array_combine() functions in PHP. Both functions are array based functions used to combine two or more arrays using PHP. We will see each function with syntax and implementation array_merge() FunctionThis function merges the two or 3 min read Creating a Zero-Filled Array in PHP Creating an array filled with zeros can be useful in various scenarios, such as initializing an array for further manipulation or setting up a default state. This article explores multiple approaches to creating a zero-filled array in PHP. A zero-filled array is an array where each element is initia 3 min read Like