1

I have a loop that is getting data from a form:

foreach ($data as $d) {
    $id = $dataEntity->getId();
    $content = $data['form['.$id.']'];
    $dataEntity->setContent($content);
}

I have one problem. In the case that form['.$id.'] is valid than the code is working. But when it is not valid I get the error message:

Notice: Undefined index: form[5207c07b25]

So I tried to change the code like this

 foreach ($data as $d) {
     $id = $dataEntity->getId();
     $formData = "form['.$id.']";
     if(isset($formData)){
         $content = $formData;
     } else {
          $content = "";
     }
     $dataEntity->setContent($content);
 }

But still I get the same error message.

yivi
  • 42,438
  • 18
  • 116
  • 138
peace_love
  • 6,229
  • 11
  • 69
  • 157

1 Answers1

1

You need to check if the index exists in the array. The index string itself will always be set:

 foreach ($data as $d) {
     $id = $dataEntity->getId();
     $formData = "form[".$id."]";
     if(isset($data[$formData])){
         $content = $data[$formData];
     } else {
          $content = "";
     }
     $dataEntity->setContent($content);
 }
JensV
  • 3,997
  • 2
  • 19
  • 43