我得到一个奇怪的PHP错误后更新我的PHP版本到5.4.0-3。

我有这样一个数组:

Array
(
    [host] => 127.0.0.1
    [port] => 11211
)

当我试图像这样访问它时,我会得到奇怪的警告

 print $memcachedConfig['host'];
 print $memcachedConfig['port'];


 Warning: Illegal string offset 'host' in ....
 Warning: Illegal string offset 'port' in ...

我真的不想只是编辑我的php.ini并重新设置错误级别。


当前回答

只使用

$memcachedConfig = array();

之前

 print $memcachedConfig['host'];
 print $memcachedConfig['port'];


 Warning: Illegal string offset 'host' in ....
 Warning: Illegal string offset 'port' in ....

这是因为你从来没有定义什么是$memcachedConfig,所以默认情况下处理字符串而不是数组。

其他回答

这对我来说很管用:

我的测试代码:

$var2['data'] = array ('a'=>'21','b'=>'32','c'=>'55','d'=>'66','e'=>'77');
foreach($var2 as $result)
{  
    $test = $result['c'];
}
print_r($test);

输出:55

看看吧,伙计们。谢谢

我认为这条消息的唯一原因是因为目标数组实际上是一个数组,如字符串等(JSON -> {"host": "127.0.0.1"})变量

在我的情况下:在获取数据库记录时,确保使用 result_array ();而不是row();或row_array ();

在检查数组之前,请执行以下操作:

if(!is_array($memcachedConfig))
     $memcachedConfig = array();

只使用

$memcachedConfig = array();

之前

 print $memcachedConfig['host'];
 print $memcachedConfig['port'];


 Warning: Illegal string offset 'host' in ....
 Warning: Illegal string offset 'port' in ....

这是因为你从来没有定义什么是$memcachedConfig,所以默认情况下处理字符串而不是数组。