Jump to content

Im confused with large csv reading line by line


Go to solution Solved by .josh,

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);
}
?>
Edited by bores_escalovsk
Link to comment
Share on other sites

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];
}
Edited by QuickOldCar
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.