Jump to content

Using Fgetcsv On Multiple Line Csv


MehItsMe

Recommended Posts

Hey all,

 

I've run into a bit of a problem when trying to use fgetcsv to dump the contents of a csv file into an array, in as much as fgetcsv stops after a line break.

I've been using fputcsv to add content to the file and every time I use fputcsv it adds a line break, hense my problem.

As you might have guessed from the time of this post (I'm in the uk) I've been trying to come up with a solution for this for quite a while; I'm pretty new to php as you might have gathered. :(

 

Code:

 

<?php

$fname=data.csv

$file=fopen($fname,"r");

$data=fgetcsv($file);

echo "$data[0] $data[1] $data[2]";

?>

 

The csv looks something like this:

adam,19

alex,16

andrew,24

etc...

I'm able to access the first 2 values with $data[0] and $data[1], but when I try and access anything after the first line break, I get undefined ofset errors.

 

If anyone could point me in the right direction to a solution for this problem I would be extremely greatful since I can't seem to find anything!!

 

In the mean time, I'm off to catch up on some sleep.

 

Cheers.

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.