1

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

12
  • What do you mean by "don't see proper data"? What do you see and what exactly do you expect? Commented Nov 30, 2011 at 2:22
  • hi, I see it as array format like this Array ( [0] => 3232456454 [1] => mike [2] => strong [3] => [email protected] [4] => 11/8/11 0:00 [5] => AU [6] => 2540 87876548788 [8] => bob [9] => cool [10] => [email protected] [11] => 11/7/11 0:00 [12] => RY [13] => 2148 23498765 Commented Nov 30, 2011 at 2:30
  • sorry, i don't know how to format the above data..so if you see, after 6th element, it takes the id of the next record. i want it as i see in the text file. Commented Nov 30, 2011 at 2:32
  • So, do you want it as an array or just as a line of text? Commented Nov 30, 2011 at 2:33
  • line of text, exactly as here 3232456454,mike,strong,[email protected],11/8/11 0:00,AU,2540 so that i will get it line by line and when i reach the 50th record or something, i will do some other stuff...i can do the count and take care of once i get the data line by line. also, how can i skip the first line..? Commented Nov 30, 2011 at 2:35

3 Answers 3

9
$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

Sign up to request clarification or add additional context in comments.

3 Comments

i did this $file = "smple.csv"; $csv = file_get_contents($file); echo $csv; just prints the last line..you should i don't have to loop, right?
@Jay file_get_contents gets the whole file at once, it should output more than just the last line.
i thought the same way, I just did what i pasted above but i see only last line..i am on mac not sure if that is an issue when i created the csv file.
0

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

1 Comment

where is your print line? if it's inside of the if (within the while) then it should fire each time
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.