Open In App

How to parse a CSV File in PHP ?

Improve
Improve
Like Article
Like
Save
Share
Report

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 given for sample data having Name and Coding Score as their column headers.

Step 2. Convert to CSV file by following the path. Go to File>Export>Change File Type> CSV Type. Save the file in your working folder with the name “Book1.csv”.

Steps for Parsing CSV file using PHP

Step 1. Create a folder and add that CSV file and create a new PHP file in it.

file path

Step 2. Open the PHP file and write the following code in it which is explained in the following steps.

  • Open dataset of CSV using fopen function.
$open = fopen("filename.csv", "r");
$data = fgetcsv($open, 1000, ",");
  • Use a loop to iterate in every row of data.
while (($data = fgetcsv($open, 1000, ",")) !== FALSE) 
{
// Read the data
}
fclose($open);

Examples to parse a CSV File in PHP

Example 1: This example describes the usage of the fclose() Method in PHP to parse a CSV File in PHP.

PHP




<?php
 
if (($open = fopen("Book1.csv", "r")) !== false) {
    while (($data = fgetcsv($open, 1000, ",")) !== false) {
        $array[] = $data;
    }
 
    fclose($open);
}
echo "<pre>";
 
// To display array data
var_dump($array);
echo "</pre>";
?>


Output:

array(6) {
[0]=>
array(2) {
[0]=>
string(5) "Name "
[1]=>
string(12) "Coding Score"
}
[1]=>
array(2) {
[0]=>
string(4) "Atul"
[1]=>
string(3) "200"
}
[2]=>
array(2) {
[0]=>
string(5) "Danny"
[1]=>
string(3) "250"
}
[3]=>
array(2) {
[0]=>
string(6) "Aditya"
[1]=>
string(3) "150"
}
[4]=>
array(2) {
[0]=>
string(7) "Avinash"
[1]=>
string(3) "300"
}
[5]=>
array(2) {
[0]=>
string(6) "Ashish"
[1]=>
string(3) "240"
}
}

Example 2: This code is another example of parsing a CSV file and returning the result in HTML tabular format.

PHP




<?php
 
$CSVvar = fopen("sampleCSVfile.csv", "r");
if ($CSVvar !== FALSE) {
?>
    <html>
    <head>
        <style>
            table,th,td {
                border: 1px solid black;
            }
        </style>
    </head>
    <div>
        <table style="border:1px solid black">
            <thead>
                <tr>
                    <th><b>Year</b></th>
                    <th><b>Mobile Brand</b></th>
                    <th><b>Total Sale</b></th>                   
                </tr>
            </thead>
<?php
    while (! feof($CSVvar)) {
        $data = fgetcsv($CSVvar, 1000, ",");
        if (! empty($data)) {
            ?>
            <tr>
                <td><?php echo $data[0];  ?></td>
                <td><div> <?php echo $data[2]?></td>
                <td><div><?php echo $data[1]; ?></div></td>
            </tr>
 <?php }?>
<?php
    }
    ?>
        </table>
    </div>
    </html>
<?php
}
fclose($CSVvar);
?>


sampleCSVfile.csv: This file is used in the above PHP file.

Output:

Parse a CSV File in PHP



Last Updated : 11 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads