开发者

php - add new index to array without generating an 'undefined index' warning

开发者 https://www.devze.com 2023-02-10 18:58 出处:网络
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.

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;
  }
  //m开发者_运维知识库ore 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消