Php 致命错误:未捕获错误:无法将字符串偏移量用作数组
一个自定义字段扩展被添加到博客插件中,导致此错误“致命错误:未捕获错误:无法将字符串偏移量用作数组”我正在使用PHP7我试图使用数组\u key\u exists(),但这并没有解决错误它意外禁用了扩展Php 致命错误:未捕获错误:无法将字符串偏移量用作数组,php,arrays,loops,foreach,php-7,Php,Arrays,Loops,Foreach,Php 7,一个自定义字段扩展被添加到博客插件中,导致此错误“致命错误:未捕获错误:无法将字符串偏移量用作数组”我正在使用PHP7我试图使用数组\u key\u exists(),但这并没有解决错误它意外禁用了扩展 public function getCustomFields() { $cfData = getXML(BLOGCUSTOMFIELDS); $cf = array('options' => '', 'main' => ''); $count_optio
public function getCustomFields()
{
$cfData = getXML(BLOGCUSTOMFIELDS);
$cf = array('options' => '', 'main' => '');
$count_options = 0;
$count_main = 0;
$count_opt = 0;
foreach($cfData->item as $custom_field)
{
if($custom_field->area == 'options')
{
$cf['options'][$count_options]['key'] = (string) $custom_field->desc;
$cf['options'][$count_options]['label'] = (string) $custom_field->label;
$cf['options'][$count_options]['type'] = (string) $custom_field->type;
$cf['options'][$count_options]['value'] = (string) $custom_field->value;
if ($custom_field->type == "dropdown")
{
$count_opt = 0;
$cf['options'][$count_options]['options'] = array();
foreach ($custom_field->option as $option)
{
$cf['options'][$count_options]['options'][] = (string) $option;
$count_opt++;
}
}
$count_options++;
}
elseif($custom_field->area == 'main')
{
$cf['main'][$count_main]['key'] = (string) $custom_field->desc;
$cf['main'][$count_main]['label'] = (string) $custom_field->label;
$cf['main'][$count_main]['type'] = (string) $custom_field->type;
$cf['main'][$count_main]['value'] = (string) $custom_field->value;
if ($custom_field->type == "dropdown")
{
$count_opt = 0;
$cf['main'][$count_main]['options'] = array();
foreach ($custom_field->option as $option)
{
$cf['main'][$count_main]['options'][] = (string) $option;
$count_opt++;
}
}
$count_main++;
}
}
return $cf;
}
根据它所指的错误
$cf=array('options'=>'','main'=>''
非常感谢你的帮助
编辑:转储XML文件
object(SimpleXMLExtended)#483(1){[“item”]=>array(8){[0]=>
对象(SimpleXMLExtended)#484(4){[“区域”]=>string(7)“选项”
[“desc”]=>string(4)“slug”[“label”]=>string(8)“slug/URL”
[“type”]=>string(4)“text”}[1]=>object(SimpleXMLExtended)#485(4)
{[“区域”]=>string(7)“选项”[“描述”]=>string(4)“标记”
[“标签”]=>string(32)“标记(用逗号分隔标记)”[“类型”]=>
字符串(4)“文本”}[2]=>对象(SimpleXMLExtended)#486(4){
[“区域”]=>string(7)“选项”[“说明”]=>string(4)“日期”[“标签”]=>
字符串(25)“发布日期(任何格式)”[“类型”]=>字符串(4)“文本”}
[3] =>对象(SimpleXMLExtended)#487(4){[“区域”]=>字符串(7)
“选项”[“描述”]=>string(8)“类别”[“标签”]=>string(30)
“将此帖子分配给类别”[“类型”]=>string(8)“下拉列表”}
[4] =>对象(SimpleXMLExtended)488(4){[“区域”]=>字符串(7)
“选项”[“描述”]=>string(6)“作者”[“标签”]=>string(14)
“作者姓名:”[“类型”]=>string(4)“文本”}[5]=>
对象(SimpleXMLExtended)#489(4){[“区域”]=>string(7)“选项”
[“desc”]=>string(7)“private”[“label”]=>string(15)”贴子是
私有“[”类型“]=>字符串(8)“复选框”}[6]=>
对象(SimpleXMLExtended)#490(4){[“区域”]=>string(4)“主”
[“desc”]=>string(5)“title”[“label”]=>object(SimpleXMLExtended)#492
(0){}[“类型”]=>string(5)“标题”}[7]=>
object(SimpleXMLExtended)#491(4){[“区域”]=>string(4)“main”
[“描述”]=>字符串(7)“内容”[“标签”]=>
对象(SimpleXMLExtended)#493(0){}[“类型”]=>string(8)“textarea”
}}}
改变
$cf=array('options'=>'',main'=>''代码>
到
$cf=array('options'=>array(),'main'=>array())代码>
错误表明
致命错误:未捕获错误:无法将字符串偏移量用作数组
您的代码错误,因为您正在尝试将新键添加到$cf['options']中,但它是字符串而不是数组
$cf['options'][$count_options] .....
改变
$cf=array('options'=>'',main'=>''代码>
到
$cf=array('options'=>array(),'main'=>array())代码>
错误表明
致命错误:未捕获错误:无法将字符串偏移量用作数组
您的代码错误,因为您正在尝试将新键添加到$cf['options']中,但它是字符串而不是数组
$cf['options'][$count_options] .....
由于PHP7.1+的发行版,无法更有效地初始化数组
从PHP7.1.0开始,对字符串应用空索引运算符会引发致命错误。以前,字符串以静默方式转换为数组。自PHP7.1+发行版以来,不太可能使用Rh初始化数组
从PHP7.1.0开始,对字符串应用空索引运算符会引发致命错误。以前,字符串以静默方式转换为数组。您能否解释一下$cfData=getXML(BLOGCUSTOMFIELDS)代码>一点?您确定使用(字符串)$选项;因为它可以在foreach循环中用任何键定义,比如$option[xxx]??看起来$option是一个数组,不能键入字符串。您应该可以看到发生错误的行号。@不可信,这是一个平面文件CMS,将数据保存在XML文件中,而不是DB。CMS被称为GetSimpleCMS,因此在这里它从与BLOGCUSTOMFIELDS相关的XML文件中查询数据section@daremachine行号是25-30这个$cf['options'][$count\u options]['key']=(字符串)$custom_field->desc代码>直到$cf['options'][$count\u options]['value']=(字符串)$custom\u字段->值代码>确定。因为,只要将代码复制粘贴到测试脚本中,我就看不到那个致命错误(为getXML生成一些输出)。所以我不知道你为什么也会犯这样的错误!你能解释一下吗代码>一点?您确定使用(字符串)$选项;因为它可以在foreach循环中用任何键定义,比如$option[xxx]??看起来$option是一个数组,不能键入字符串。您应该可以看到发生错误的行号。@不可信,这是一个平面文件CMS,将数据保存在XML文件中,而不是DB。CMS被称为GetSimpleCMS,因此在这里它从与BLOGCUSTOMFIELDS相关的XML文件中查询数据section@daremachine行号是25-30这个$cf['options'][$count\u options]['key']=(字符串)$custom_field->desc代码>直到$cf['options'][$count\u options]['value']=(字符串)$custom\u字段->值代码>确定。因为,只要将代码复制粘贴到测试脚本中,我就看不到那个致命错误(为getXML生成一些输出)。所以我不知道你为什么也会犯这样的错误!