0

I am trying to run this code:

$file = fopen($txtFile, "r");    
    while(!feof($file)) {    
        $line = fgets($file);    
        $pieces = explode(",", $line);    

    $date = $pieces[0];
    $open = $pieces[1];
    $high = $pieces[2];
    $low = $pieces[3];
    $close = $pieces[4];
    $volume = $pieces[5];
}

and I get this notice:

Undefined offset: 1 in ...
Undefined offset: 2 in ...
Undefined offset: 3 in ...
Undefined offset: 4 in ...
Undefined offset: 5 in ...

Why?

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

2

As Stephen said it looks like the array is not what you are expecting it to be, there are a few things you can do.

Try using

var_dump($pieces)

and take a look at what the array actually contains. Another thing you can do to prevent errors and be more defensive in your code is something like the following:

$file = fopen($txtFile, "r");    
while(!feof($file)) {    
    $line = fgets($file);    
    $pieces = explode(",", $line);    

if(isset($pieces[0]))
    $date = $pieces[0];
if(isset($pieces[1]))
    $open = $pieces[1];
if(isset($pieces[2]))
    $high = $pieces[2];
if(isset($pieces[3]))
    $low = $pieces[3];
if(isset($pieces[4]))
    $close = $pieces[4];
if(isset($pieces[5]))
    $volume = $pieces[5];
}
}

Alternatively in this case you can just check the length of the $pieces, which for your use may be better and shorter, like this:

$file = fopen($txtFile, "r");    
while(!feof($file)) {    
    $line = fgets($file);    
    $pieces = explode(",", $line);    

if(sizeof($pieces) != 6){
  //handle this case here
}
else
{
    $date = $pieces[0];
    $open = $pieces[1];
    $high = $pieces[2];
    $low = $pieces[3];
    $close = $pieces[4];
    $volume = $pieces[5];
}
}

This just ensures that the variables exist before try to do anything with them and will avoid the issue of undefined index.

Jake
  • 513
  • 5
  • 16
  • Given that they probably need all those values, it might just be better to check if `count($pieces)` is less than 6 – StephenTG Aug 15 '13 at 18:38
  • Ya you're probably right, and it's certainly less code. the benefit of doing them individually is the ability to handle each case differently though. – Jake Aug 15 '13 at 18:40
  • 1
    I agree that your method is more robust, but from what I inferred from the question, a length check is probably all that's needed. I'd mention both. – StephenTG Aug 15 '13 at 18:46
  • Already solved it, one of the downloaded CSV files did not contain proper dates. Anyway, thanks for the help :) –  Aug 15 '13 at 19:03
  • 1
    In that case, the answer here still would have helped you get that so you should either accept an answer, or edit your post with your solution and mark the question as solved – Jake Aug 15 '13 at 19:08
0

I assume your file has the following format:

date,open,high,low,close,volume

date,open,high,low,close,volume

I think the issue is that your file isn't formatted correctly.

One way to debug the problem is to var_dump( $pieces );

Also I think you forgot to close the file handle by calling fclose( $file );

Here's a much simpler and OOP solution:

$file = new SplFileObject( '/home/jason/file.csv', 'r' );
$file->setFlags( SplFileObject::READ_CSV );

foreach ( $file as $lineNumber => $line ) {

    var_dump( $line ); // $line is an array

}

For more details, read http://www.php.net/manual/en/class.splfileobject.php

Hope this helps :)

Jason Gu
  • 104
  • 4