如何避免PHP注意“未定义偏移量:0”而不检查数组的每个字段

时间:2022-10-28 22:50:49

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:

可能有些字段没有设置,这没问题。事实上,我得到了这个PHP通知:

Notice: Undefined offset: 0 in bafa_insert_keyvisual() ...........

注意:未定义的偏移量:0 in bafa_insert_keyvisual()

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

谢谢你的帮助

5 个解决方案

#1


13  

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...

等等……

#2


43  

No there is not

没有没有

You can do an isset():

您可以执行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:

或者如果你知道你只检查指标0

$array = $array + array(null);

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

如果原始的$array[0]是未设置的,现在它是空的

#3


0  

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'];
?>

#4


0  

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

您应该使用“isset”函数来检查字段的存在性,实际上这是一个很好的编码实践。

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

#5


-1  

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:

老实说,行数据对象的结构似乎不太方便而且容易出错。特别是如果你在其他环境中使用$row,我建议你将它封装在一个对象中:

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.

如果在$row->field_field_video中只有一个值,那么它不应该是数组。如果奇怪的数据结构来自远程数据源或类似的数据源,我将使用包装器类作为一种反损坏层。否则,它将在您的while应用程序中以$row-> field_video[0]…到处都是。

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

我知道这不是你想要的快速而简单的解决方案,但是吞咽错误从来都不是一个好主意。

#1


13  

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...

等等……

#2


43  

No there is not

没有没有

You can do an isset():

您可以执行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:

或者如果你知道你只检查指标0

$array = $array + array(null);

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

如果原始的$array[0]是未设置的,现在它是空的

#3


0  

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'];
?>

#4


0  

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

您应该使用“isset”函数来检查字段的存在性,实际上这是一个很好的编码实践。

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

#5


-1  

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:

老实说,行数据对象的结构似乎不太方便而且容易出错。特别是如果你在其他环境中使用$row,我建议你将它封装在一个对象中:

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.

如果在$row->field_field_video中只有一个值,那么它不应该是数组。如果奇怪的数据结构来自远程数据源或类似的数据源,我将使用包装器类作为一种反损坏层。否则,它将在您的while应用程序中以$row-> field_video[0]…到处都是。

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

我知道这不是你想要的快速而简单的解决方案,但是吞咽错误从来都不是一个好主意。