0

Previously my code looked like this:

for ($i=0, $n=sizeof($values); $i<$n; $i++) {
  $field .= '<option value="' . xtc_parse_input_field_data($values[$i]['id'], array('"' => '&quot;')) . '"';
  if ($default == $values[$i]['id']) {
    $field .= ' selected="selected"';
  }

  $field .= '>' . xtc_parse_input_field_data($values[$i]['text'], array('"' => '&quot;', '\'' => '&#039;', '<' => '&lt;', '>' => '&gt;')) . '</option>';
}

Following some advice I changed it to:

if (is_array($values) && count($values) > 0) {
  foreach ($values as $value) {
    $field .= '<option value="' . xtc_parse_input_field_data($value['id'], array('"' => '&quot;')) . '"';
    if ($default == $value['id']) {
      $field .= ' selected="selected"';
    }

    $field .= '>' . xtc_parse_input_field_data($value['text'], array('"' => '&quot;', '\'' => '&#039;', '<' => '&lt;', '>' => '&gt;')) . '</option>';
  }
}

But the error remains:

Notice: Undefined index: id in E:\xampp\htdocs\testshop\inc\xtc_draw_pull_down_menu.inc.php

What's causing this error?

John Hascall
  • 9,176
  • 6
  • 48
  • 72
Ronny Linsener
  • 253
  • 3
  • 16
  • Did you try `var_dump($values)`? – Lewis May 18 '14 at 18:32
  • Also: does the error message contain a line number? If so, which line is it? – Stefan Schmiedl May 18 '14 at 18:49
  • 1
    I'd like to remind everyone that this question is close to 2 years old before asking for any clarification. Chances are that OP will not be able to tell you what `$values` looked like. – h2ooooooo Jan 26 '16 at 12:50
  • 1
    In the code shown, this error could only come from two lines, both of which make the same access `$value['id']`. The way this code is structured, the values array needs to be a numeric array of associative arrays. For example, created with `$values[] = array( "id" => "blah", "text" => "bleah");` – John Hascall Jan 26 '16 at 12:52
  • Yes, I was just cleaning this up for future visitors because it was a top result from a google search I did. – John Hascall Jan 26 '16 at 12:54
  • @JohnHascall I believe the regular reference we use is [PHP: “Notice: Undefined variable” and “Notice: Undefined index”](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – h2ooooooo Jan 26 '16 at 12:54

1 Answers1

0

I would say that the warning basically does not have anything to do with the piece of code that you have pasted, rather it has to do with the fact that $value does not have an index named id in the first place. So in order to fix it you have to check at the source - the place where $values is generated/stored, to see why there are some values without id.

Having said that, in order to prevent the unwanted notice in your code you can add the following line to test if an index named id exists, before proceeding:

if(array_key_exists('id',$value)) {
   //do all your stuff
}

You can add this just below the line after foreach ($values as $value) { so that everything you do inside the loop only happens if $value has an id element.

However as stated earlier, if you expect every $value to definitely have an id, go back to the place where $values are generated and check whats going on there.

raidenace
  • 12,789
  • 1
  • 32
  • 35