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

I iterate over the array,

If I find an error in the array, I add a key called error.

however, I get a whole bunch of 'undefined index' error warnings.

How can I do this without generating these alerts?


Code on request
$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
}


An example of what the string $ in 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)


Have a look? Full size is http://webspirited.com/proof.png
+3
source share
1

== =.

undefined. $row['error'] , E_NOTICE undefined.

+2

Source: https://habr.com/ru/post/1792695/


All Articles