[英]Fatal error: Uncaught Error: Cannot use string offset as an array
A custom field extension is added to a blog plugin causing this error "Fatal error: Uncaught Error: Cannot use string offset as an array" I'm using PHP7 I tried to use array_key_exists() but this didn't solve the error it accidentally disabled the extension自定义字段扩展被添加到博客插件导致此错误“致命错误:未捕获错误:无法将字符串偏移用作数组”我正在使用 PHP7 我尝试使用 array_key_exists() 但这并没有解决它意外的错误禁用扩展
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;
}
According to the error it's referring to根据它所指的错误
$cf = array('options' => '', 'main' => ''); $cf = array('options' => '', 'main' => '');
Your help is much appreciated.非常感谢您的帮助。
EDIT: Dumping the XML File编辑:转储 XML 文件
object(SimpleXMLExtended)#483 (1) { ["item"]=> array(8) { [0]=> object(SimpleXMLExtended)#484 (4) { ["area"]=> string(7) "options" ["desc"]=> string(4) "slug" ["label"]=> string(8) "Slug/URL" ["type"]=> string(4) "text" } [1]=> object(SimpleXMLExtended)#485 (4) { ["area"]=> string(7) "options" ["desc"]=> string(4) "tags" ["label"]=> string(32) "Tags (separate tags with commas)" ["type"]=> string(4) "text" } [2]=> object(SimpleXMLExtended)#486 (4) { ["area"]=> string(7) "options" ["desc"]=> string(4) "date" ["label"]=> string(25) "Publish date (any format)" ["type"]=> string(4) "text" } [3]=> object(SimpleXMLExtended)#487 (4) { ["area"]=> string(7) "options" ["desc"]=> string(8) "category" ["label"]=> string(30) "Assign This Post To A Category" ["type"]=> string(8) "dropdown" } [4]=> object(SimpleXMLExtended)#488 (4) { ["area"]=> string(7) "options" ["desc"]=> string(6) "author" ["label"]=> string(14) "Author's Name:" ["type"]=> string(4) "text" } [5]=> object(SimpleXMLExtended)#489 (4) object(SimpleXMLExtended)#483 (1) { ["item"]=> array(8) { [0]=> object(SimpleXMLExtended)#484 (4) { ["area"]=> string(7) "options " ["desc"]=> string(4) "slug" ["label"]=> string(8) "Slug/URL" ["type"]=> string(4) "text" } [1]= > object(SimpleXMLExtended)#485 (4) { ["area"]=> string(7) "options" ["desc"]=> string(4) "tags" ["label"]=> string(32) "标签(用逗号分隔标签)" ["type"]=> string(4) "text" } [2]=> object(SimpleXMLExtended)#486 (4) { ["area"]=> string(7) "options" ["desc"]=> string(4) "date" ["label"]=> string(25) "Publish date (any format)" ["type"]=> string(4) "text" } [3]=> object(SimpleXMLExtended)#487 (4) { ["area"]=> string(7) "options" ["desc"]=> string(8) "category" ["label"]= > string(30) "Assign this Post To A Category" ["type"]=> string(8) "dropdown" } [4]=> object(SimpleXMLExtended)#488 (4) { ["area"]=> string(7) "options" ["desc"]=> string(6) "author" ["label"]=> string(14) "Author's Name:" ["type"]=> string(4) "text " } [5]=> 对象(SimpleXMLExtended)#489 (4) { ["area"]=> string(7) "options" ["desc"]=> string(7) "private" ["label"]=> string(15) "Post is private" ["type"]=> string(8) "checkbox" } [6]=> object(SimpleXMLExtended)#490 (4) { ["area"]=> string(4) "main" ["desc"]=> string(5) "title" ["label"]=> object(SimpleXMLExtended)#492 (0) { } ["type"]=> string(5) "title" } [7]=> object(SimpleXMLExtended)#491 (4) { ["area"]=> string(4) "main" ["desc"]=> string(7) "content" ["label"]=> object(SimpleXMLExtended)#493 (0) { } ["type"]=> string(8) "textarea" } } } { ["area"]=> string(7) "options" ["desc"]=> string(7) "private" ["label"]=> string(15) "Post is private" ["type"] => string(8) "checkbox" } [6]=> object(SimpleXMLExtended)#490 (4) { ["area"]=> string(4) "main" ["desc"]=> string(5) "title" ["label"]=> object(SimpleXMLExtended)#492 (0) { } ["type"]=> string(5) "title" } [7]=> object(SimpleXMLExtended)#491 (4) { ["area"]=> string(4) "main" ["desc"]=> string(7) "content" ["label"]=> object(SimpleXMLExtended)#493 (0) { } ["type "]=> string(8) "textarea" } } }
Change改变$cf = array('options' => '', 'main' => '');
to到$cf = array('options' => array(), 'main' => array());
The error says that错误说
Fatal error: Uncaught Error: Cannot use string offset as an array致命错误:未捕获错误:无法使用字符串偏移量作为数组
Your code is wrong because you are trying add new key into $cf['options'] but it is string and not array您的代码是错误的,因为您正在尝试将新键添加到 $cf['options'] 中,但它是字符串而不是数组
$cf['options'][$count_options] .....
Since the release PHP 7.1+, is not more possible to initialize an array witrh As of PHP 7.1.0, applying the empty index operator on a string throws a fatal error.从 PHP 7.1+ 版本开始,不再可能用 7.1.0 来初始化数组,在字符串上应用空索引运算符会引发致命错误。 Formerly, the string was silently converted to an array.以前,字符串被默默地转换为数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.