Jump to content

Im confused with large csv reading line by line


Recommended Posts

i have to read a single line from a csv,  its a really big file and i only need one column.

i need the response to be a string ,i made a search and found the following code but i dont have any idea how to get a single line from a single string per run .

<?php
$row = 1;
//open the file
if (($handle = fopen("file.csv", "r")) !== FALSE)
{
    while (($data = fgetcsv($handle, 0, ",")) !== 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";
        }
    }
    fclose($handle);
}
?>

if you need to find something specific, wrap it with an if statement, perform any search or checks on it and then echo or save as a variable.

 

examples:

if($data[$c] != ''){
$string = $data[$c];
echo $string . "<br />\n";
}

or if to make them a new array

$new_array = array();//outside the loop


if($data[$c] != ''){
$new_array[] = $data[$c];
}

what are you trying to do... are you trying to just echo out the value of only 1 column? Are you trying to put it into a db?

its a part of my code , the echo is only for testing ...the code needs to read 1 line per run ,return the value as a string and at the next run read the next line.

this is worst than the time i learned regex ,-,

 

If it's a large file you should save the csv to a database and query each result that way

Having to load a very large file each time takes lots of resources.

 i was trying to do that, but the file have 2gb , not the easyest thing

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.