我得到一个奇怪的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并重新设置错误级别。


当前回答

为PHP

//Setup Array like so
$memcachedConfig = array(
  "host" => "127.0.0.1",
  "port" => "11211"
);

//Always a good practice to check if empty

if(isset($memcachedConfig['host']) && isset($memcachedConfig['port'])){

    //Some codes

    print_r ($memcachedConfig['host']);
    print_r ($memcachedConfig['port']);

}

只要确保返回的值不为空。 这个例子是PHP的看看如何在其他语言中检查数组是否为空。

其他回答

为PHP

//Setup Array like so
$memcachedConfig = array(
  "host" => "127.0.0.1",
  "port" => "11211"
);

//Always a good practice to check if empty

if(isset($memcachedConfig['host']) && isset($memcachedConfig['port'])){

    //Some codes

    print_r ($memcachedConfig['host']);
    print_r ($memcachedConfig['port']);

}

只要确保返回的值不为空。 这个例子是PHP的看看如何在其他语言中检查数组是否为空。

只是以防它帮助任何人,我得到这个错误,因为我忘记反序列化一个序列化的数组。如果这适用于你的情况,我肯定会检查一下。

在我的情况下,我把mysql_fetch_assoc改为mysql_fetch_array并解决。它需要3天来解决:-(和我的项目的其他版本运行fetch assoc。

我通过使用trim()函数解决了这个问题。问题在于空间。

让我们试试

$unit_size = []; //please declare the variable type 
$unit_size = exolode("x", $unit_size);
$width  = trim ($unit_size[1] );
$height = trim ($unit_size[2] );

我希望这对你有所帮助。

只使用

$memcachedConfig = array();

之前

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


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

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