4

I am looping over an array,

if I spot an error in the array I am adding a key called error.

however I am getting a whole pile of 'undefined index 'error' warnings.

How can I do this without generating those warnings?


Code as requested

$csv = array();
if (($handle = fopen($filePath, "r")) !== FALSE) {
  while (($csv[] = fgetcsv($handle)) !== FALSE);
  fclose($handle);
}

foreach ($csv as &$row) {
  if (count($row) > $maxCols)
    $maxCols = count($row);

  if (count($row) == 0) {
    $errors++;
    $row['error'] = 'Empty Column!';
    continue;
  }
  //more code
}


Example of what $row would look like in the foreach()

Array (
[0] => 1
[1] => 12
[2] => 64273566141
[3] => bakery
[4] => 2009-12-08 09:07:39
[5] => 2009
[6] => 2009-12-08 09:08:35
[7] =>
[8] => 0 )


See? Full Size http://webspirited.com/proof.png

11
  • 1
    can you please show some code? Commented Feb 17, 2011 at 1:27
  • and no, I cant add an empty index when I create the array as it comes from fgetcsv Commented Feb 17, 2011 at 1:27
  • isset or array_key_exists are the usual solutions to this error, but code please, cause given what you are describing you shouldn't be getting this error unless i've misunderstood what you are asking Commented Feb 17, 2011 at 1:30
  • 1
    that code shouldn't produce an error... I think the error is more likely to be where you are checking for the error later (i.e. expand the //more code comment please) Commented Feb 17, 2011 at 1:33
  • Where is the E_NOTICE error occurring? In the //more code section? Nothing you have there would throw an undefined index error. Commented Feb 17, 2011 at 1:36

1 Answer 1

2

You're using == instead of =.

Using the comparison operator is attempting to fetch the potentially undefined array index. If $row['error'] has not been set previously, this will trigger an E_NOTICE undefined index error.

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

2 Comments

nope, Im adding the error index to all 'rows' of the csv array that have errors. Please see my code above
@Hailwood Edited my answer given the new information in your question

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.