JDesigns
JDesigns

Reputation: 2332

parse csv file using php line by line

I have a csv file with data row 1 is header like this

 id fname lname email date code pid

each row then has data below each field name

3232456454  mike    strong  [email protected]   11/8/11 0:00    AU  2540

and when i see this csv using text editor, i see it as below.

3232456454,mike,strong,[email protected],11/8/11 0:00,AU,2540
87876548788,bob,cool,[email protected],11/8/11 0:00,RY,2148
23498765,nike,tick,[email protected],11/8/11 0:00,TE,5240

Now i want to read the file using php and i want the data to be exactly same format as text file. I will use this data for other purpose as it is. I don't want to split at commas so i cannot use fgetcsv. I tried by just opening it using php file() but when i loop it, i don't see the proper data. Can some one please throw some suggestions?

if I do this way, it prints in the array with each element taking one value and I don't want this..

    $csv = array();
    $lines = file('sample.csv', FILE_IGNORE_NEW_LINES);

    foreach ($lines as $key => $value)
    {
        $csv[$key] = str_getcsv($value);
    } 
print_r($csv)

I tried using parsecsv library from google code but this is not really necessary as i want the data line by line.

I need to ignore the first line(header) in the output.

If i can get the output like this, this will solve my issues

Array
(
    [id,fname,lname,email,,data,code,pid] => 3232456454,mike,strong,[email protected],11/8/11 0:00,AU,2540
)

Array
(
    [id,fname,lname,email,,data,code,pid] => 87876548788,bob,cool,[email protected],11/8/11 0:00,RY,2148
)

regards

Upvotes: 1

Views: 24323

Answers (3)

Pierre-Yves
Pierre-Yves

Reputation: 1

If no matter what you do you can only get the last line of your CSV file using fgets() or fgetcsv(), simply try to copy/paste the content of your file to a new one and try using it instead.

I'm referring to the issue that @Jay seems to had: It seems that if the file was created with Excel 2011/Mac, its structure is not correctly interpreted.

See this for more info: Which encoding opens CSV files correctly with Excel on both Mac and Windows?

Upvotes: 0

deceze
deceze

Reputation: 522081

$csv = file_get_contents($file);

This will get the file as is without any conversions or splitting or anything, just one long string.

If you want to split it into an array for each line, simply do

$csv = file($file, FILE_IGNORE_NEW_LINES);

and nothing else. No looping, no getcsv, just file().


But even if you say you're not interested in parsing the CSV, you are dealing with a CSV so I'd parse it as soon as possible and output it again, even back to CSV if necessary.

$fh = fopen($file, 'r');
$header = fgetcsv($fh);

$data = array();
while ($line = fgetcsv($fh)) {
    $data[] = array_combine($header, $line);
}

fclose($fh);

print_r($data);


// outputting:

$out = fopen('php://output', 'w');
fputcsv($out, array_keys($data[0])); // skip this if you don't want headers
foreach ($data as $line) {
    fputcsv($out, $line);
}
fclose($out);

If line endings aren't properly recognized, try ini_set('auto_detect_line_endings', true); on top of your script.

See here for a demo: http://codepad.viper-7.com/t31IEv

Upvotes: 9

Robot Woods
Robot Woods

Reputation: 5687

Use fopen and fgets instead, and use a counter of which line you're at so you can ignore the first:

$c=0;
$data=fopen($file,'r');
while($row=fgets($data)){
    //$row is your line as a string
    if($c!=0){
    //do something with it
    echo $row."\n";
   }
   $c++;
}

Upvotes: 0

Related Questions