在 PHP 7.4 中报错:Illegal offset type in isset or empty
1、在 PHP 7.4 中报错:Illegal offset type in isset or empty。如图1
{ "errors": [ { "debugMessage": "Illegal offset type in isset or empty", "message": "Internal server error", "extensions": { "category": "internal" }, "locations": [ { "line": 3, "column": 5 } ], "path": [ "onlineStore", "theme" ], "trace": [] } ], "data": { "onlineStore": { "theme": null, "__typename": "OnlineStore" }, "__typename": "Query" } }
2、代码如下:
if (isset($setting['settingId']) && isset($currentSettings[$setting['settingId']])) { $setting = $setting + [ 'value' => $currentSettings[$setting['settingId']], ]; }
3、打印 $setting,其值为对象。如图2
Array ( [info] => 轮播图标题、模块标题、商品的标题等 [label] => [type] => HEADER [default] => [content] => 标题 [id] => Modules\OnlineStoreThemeGraphQL\Resolver\Id Object ( [prefixes:protected] => Array ( [0] => 96b1c8b9-5b18-4760-9e26-50ab009ac011 [1] => settings [2] => 0 ) ) [__typename] => ThemeSettingHeader [settingId] => Modules\OnlineStoreThemeGraphQL\Resolver\Id Object ( [prefixes:protected] => Array ( [0] => 96b1c8b9-5b18-4760-9e26-50ab009ac011 [1] => settings [2] => 0 ) ) )
4、将 $setting[‘settingId’] 转为 字符串。
$settingId = isset($setting['settingId']) ? (string) $setting['settingId'] : null; // 获取当前设置值 if (isset($settingId) && isset($currentSettings[$settingId])) { $setting = $setting + [ 'value' => $currentSettings[$setting['settingId']], ]; }
5、打印转换后的字符串值如下
96b1c8b9-5b18-4760-9e26-50ab009ac011/settings/0
6、不再报错。
近期评论