Jump to content

Recommended Posts

I'm trying to read in a csv file and put it into arrays.  Shouldn't be too hard... but for some reason, my loop only reads in the first line of the .csv file.  Any suggestions?

 

$eday_count = 0;
if(($fp=fopen($event_fn, 'r'))!=null) {
  while(($line=fgetcsv($fp,500,",","\""))!==FALSE) {
       $event_day[$eday_count]['type'] = $line[$sch_TYPE];
       $event_day[$eday_count]['msg'] = $line[$sch_MSG];
       $eday_count++;
    }
}

 

After this, my $event_day array only has one entry, and it should have several.  Any help would be greatly appreciated.

I tried this:

$line=fgetcsv($fp);
print_r($line);

 

The result looks like there's no endline at the end of each line, so fgetcsv is reading the entire file as if it's a single line.  The first field of a line is appended onto the last field of the line above it into a single array field.  How can I fix this? (preferably without editing the .csv, but I may have to...)

 

Thanks!

 

Just a little more info:

 

The fgetcsv() output looks like this (one function call, should only return one line):

Array ( [0] => 9/15/07 [1] => 0:00 [2] => 0:00 [3] => Public [4] => test 9/15/07 [5] => 0:00 [6] => 0:00 [7] => Public [8] => test2 9/20/07 [9] => 0:00 [10] => 5:07 [11] => Private [12] => sg 9/23/07 [13] => 0:01 [14] => 5:07 [15] => Private [16] => sg 9/23/07 [17] => 14:04 [18] => 5:07 [19] => Private [20] => sg)

 

When it should be like this (multiple calls shown):

Array ( [0] => 9/15/07 [1] => 0:00 [2] => 0:00 [3] => Public [4] => test )

Array ( [0] => 9/15/07 [1] => 0:00 [2] => 0:00 [3] => Public [4] => test2 )

Array ( [0] => 9/20/07 [1] => 0:00 [2] => 5:07 [3] => Private [4] => sg )

Array ( [0] => 9/23/07 [1] => 0:01 [2] => 5:07 [3] => Private [4] => sg )

Array ( [0] => 9/23/07 [1] => 14:04 [2] => 5:07 [3] => Private [4] => sg  )

 

I'm still quite confused.  Does anyone know why it might be doing this?  What line delimeter does a .csv use & what is fgetcsv expecting?  Could it have something to do with the fact that the .csv was created on a PC and the script is running on a Mac?  I would really appreciate some help with this one.

 

 

Alright, it's been figured out, in case anyone else has a similar problem:  DON'T USE EXCEL FOR YOUR CSV FILES!  Somehow Excel messed up the newlines in the file, so it exploded.  Recreate your file in notepad or something similar.

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.