开发者

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

i have the following array:

    $keyvisual_data = array(
        'video_file'            => $row->field_field_video[0]['rendered']['#item']['uri'],
        'bild_file'             => $row->field_field_bild[0]['rendered']['#item']['uri'],
        'subline_src'           => $row->_field_data['nid']['entity']->field_key_titel['und'][0]['safe_value'],
        'screenreader_src'      => $row->field_field_alt_screenreader[0]['rendered']['#markup'],
        'alt_src'               => $row->field_field_bild[0]['rendered']['#item']['alt']
    );

it might happen that some of the fields are not set, this is okay. in fact i am getting this PHP notice:

Notice: Undefined offset: 0 in bafa_insert_keyvisual() .........开发者_StackOverflow..

is it somehow possible to assign a default value to each key in case it is undefined WITHOUT checking each field of the array manually?

thanks for help


No there is not

You can do an isset():

if(isset($array[0])){
    echo $array[0];
}
else {
  //some error?
}

Or if you know that you are only going to be checking index 0:

$array = $array + array(null);

So if the original $array[0] was unset, now it is null


Yes, add @ before the field like:

$keyvisual_data = array(
    'video_file'            => @$row->field_field_video[0]['rendered']['#item']['uri'],
    'bild_file'             => @$row->field_field_bild[0]['rendered']['#item']['uri'],
    'subline_src'           => @$row->_field_data['nid']['entity']->field_key_titel['und'][0]['safe_value'],
    'screenreader_src'      => @$row->field_field_alt_screenreader[0]['rendered']['#markup'],
    'alt_src'               => @$row->field_field_bild[0]['rendered']['#item']['alt']
);

and then initialize the nulls:

if($keyvisual_data['video_file'] === null)
    $keyvisual_data['video_file'] = $default_video_file;

etc...


You could add a shorthand check if each value is null

$default_value = "Default value";

$keyvisual_data = array(
        'video_file' => ($row->field_field_video[0]['rendered']['#item']['uri'] == null) ? $default_value : $row->field_field_video[0]['rendered']['#item']['uri']

// etc.. etc...
);

A less messy version of this, to explain the code more clearly:

<?php

$default_value = "I am a Default value";

$video_file = null;

$new_array = array(
        'video_file' => ($video_file == null) ? $default_value : $video_file
);

// Will output "I am a Default value"
echo $new_array['video_file'];
?>


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'] = "";
}


To be honest, the structure of the row data object doesnt seem very convinient and error prone. Especially if you are using $row in some other context, too, I would suggest you wrap it in an object:

class RowWrapper
{
    private $_row;

    public function __construct($row)
    {
        $this->_row = $row;
    }

    public function getVideoFile()
    {
        if (!isset($row->field_field_video[0])) {
            return null;
        }

        return $row->field_field_video[0]['rendered']['#item']['uri'];
    }

    ...
}

$rowWrapper = new RowWrapper($row);

$keyvisual_data = array(
    'video_file'            => $rowWrapper->getVideoFile(),
    ...
);

If you there is only one value e. g. inside of $row->field_field_video then it shouldnt be an array. I would use a wrapper class as a kind of anti corruption layer if that strange datastructure comes from a remote source or the like. Otherwise it will creep through your while application with $row->field_field_video[0]... stuff all over the place.

I know this is not the fast and easy solution you want, but swallowing errors is never a good idea.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜