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

Questions:

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.

<?php
    $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";
            $row++;
            for ($c=0; $c < $num; $c++) {
                echo $data[$c] . "<br />\n";
               // echo $data[1];
            }
        }
        fclose($handle);
    }
    ?>

Remember to put your csv file location in the code

Hope that helps

Answer:

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];
}

var_dump($object);

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];
}

var_dump($object);

Like so:

id,name,city,username,contact_id,phone

and

1,Test,chicage,testuser,21,123456789

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:

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

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.

Answer:

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.

http://www.php.net/manual/en/function.fgetcsv.php

http://www.php.net/manual/en/function.fputcsv.php

http://www.php.net/manual/en/function.str-getcsv.php

Answer:

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);

var_dump($data);

Answer:

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