Home » excel » php – Get each row data from CSV and then their data cells in variable

php – Get each row data from CSV and then their data cells in variable

Posted by: admin April 23, 2020 Leave a comment


My CSV has table like

Now, what I want to do is, for each record, I want to get the respective cell data and take them in variables. (I need to perform some tasks with the variables then).

e.g. (for each row, their values in variables)

$id = 1;
$name = 'Test';

and so on

Array could be used to do this.

For example,
$row_1 = (‘1’, ‘Test’, ‘chicago’, ‘testuser’, ’21’, ‘123456789’)

I tried searching google, but the results were not relevant to my query.

How to&Answers:

So you can run a for loop over each row and get it to return the data from each row and cell.

    $row = 1;
    if (($handle = fopen("data.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);
            echo "<p> $num fields in line $row: <br /></p>\n";
            for ($c=0; $c < $num; $c++) {
                echo $data[$c] . "<br />\n";
               // echo $data[1];

Remember to put your csv file location in the code

Hope that helps


doh… thought it was JavaScript..

But think I can try in PHP: http://codepad.org/8SBgTCYy

$labels = explode(",", "id,name,city,username,contact_id,phone");
$values = explode(",", "1,Test,chicage,testuser,21,123456789");

$i = 0;
$count = count($labels);
$object = array();
for ( ; $i < $count; $i++ ) {
    $object[$labels[$i]] = $values[$i];


and the other one: http://codepad.org/AmVp08QR

$values = explode(",", "id,1,name,Test,city,chicago,username,testuser,contact_id,21,phone,123456789");

$i = 0;
$count = count($values);
$object = array();

for( ; $i < $count; $i++ ) {
    $object[$values[$i]] = $values[$i+1];


Like so:




var labels = "id,name,city,username,contact_id,phone".split(","),
    values = "1,Test,chicage,testuser,21,123456789".split(","),

    object = {},
    i = 0,
    len = labels.length;

for( ; i < len; i++) object[labels[i]] = values[i];

Now you have and Object named object as:

    id: "1",
    name: "Test",
    city: "chicago",
    username: "testuser",
    contact_id: "21",
    phone: "123456789"

or like this:


var values = "id,1,name,Test,city,chicago,username,testuser,contact_id,21,phone,123456789".split(","),

    object = {},
    i = 0,
    len = values.length;

for ( ; i < len; i+=2 ) {
    object[values[i]] = values[i+1];

Then you end up with the same Object in the end.


Take a look at fgetcsv() and family…

Also, your CSV files better be compliant with CSV standards; I’ve found that a lot of CSV files are generated using non-standard conventions… it makes interop very painful.





array_combine() might be helpful here:

$labels = explode(",", "id,name,city,username,contact_id,phone"); 
$values = explode(",", "1,Test,chicage,testuser,21,123456789"); 
//  Use fgetcsv() to read these from your file

$data = array_combine($labels,$values);



I adopted my own technique, by writing each line comma separated.