Saturday, July 21, 2012

CSV file handling in PHP

CSV stands for "comma seperated values". CSV files look like excel spredsheets and are quite useful in storing data and also it is quite simple to parse a CSV file. By definition, a CSV file contains rows of data seperated by commas and the end of a row is marked by a newline character (this means that 1 line contains 1 row and values in a row are seperated by commas). You can think of a CSV file as a table of values. For example, a CSV file might look like this:

Mark, 25, Apple
Jack, 29, Google
Chet, 31, Facebook

The above CSV file stores information about some people like their names, ages and the companies they work in. This CSV file can be though of as a table with 3 rows and 3 columns. Most websites use CSV files to make some data programmatically available to the public. (For example, Yahoo! Finance. Yahoo! Finance makes stock available to other wesites in a CSV file) You have to open the CSV file from your PHP code and parse the file to read the data or use it on your own website.

Thankfully, PHP provides inbuilt functions to parse CSV files. The first step, of course, is opening the file using fopen() for reading. Assuming that the name of the CSV file is users.csv you can write

$handle = fopen("users.csv", "r");

Next, to get the a line (i.e a row) from a CSV file we use the function fgetcsv().

$data = array();
$data = fgetcsv($handle);

The above snippet of code reads the next line (row) from the csv file referred to by $handle and strores the values in an array called $data. Thus the return type of this function is an array. The function can take more parameters but they are optional. You can read more about fgetcsv() at php.net. The rest of the parameters are hardly of any use in simple, regular applications. To make sense of the data we retrieve using a CSV file we need to know the column order and the number of columns. This means, if you are parsing a CSV file that someone else has given you, you must take some time and figure out what each column denotes and the number of columns. After getting a row of the CSV file into an array, we can use the values of cells by getting the value at a particular index of the array. For example, $data[0] refers to the value of the first cell in the row stored in the array $data.

For a CSV file having multiple rows we can put the above lines of code inside a while loop. For example, we can do this:

$data = array();

// Open the file....

while (1)
{
    $data = fgetcsv($handle);
    if (data == NULL) // EOF has been reached
        break;

    // Print the data....
}

Writing data to a CSV file (i.e. generating a CSV file) is even easier. The first step is creating an array (representing a row of data) and putting some data into it. Then open file to write to using fopen() and use the function fputcsv to write a row of data to a CSV file.

$row = array();

// Put some data into the array.

$handle = fopen("mycsv.csv", "w"); // Open the file
fpucsv ($handle, $row);

More about fputcsv() at php.net

No comments:

Post a Comment