How to avoid PHP Notice “Undefined offset: 0” without checking each field of array

后端 未结 5 1161
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 03:27

i have the following array:

    $keyvisual_data = array(
        \'video_file\'            => $row->field_field_video[0][\'rendered\'][\'#item\'][\'uri\'],         


        
5条回答
  •  后悔当初
    2021-02-05 03:51

    You should use "isset" function to check the existence of the field, and infact its a good coding practice..

    $keyvisual_data = array(
          'video_file' => isset($rowWrapper->getVideoFile()) ? $rowWrapper->getVideoFile() : "any default value",
           ...
     );
    

    or you can use it in this way :

    if(isset($rowWrapper->getVideoFile()))
    {  
         $row['video_file'] = $rowWrapper->getVideoFile();
    }
    else {
       $row['video_file'] = "";
    }
    

提交回复
热议问题