非法字符串偏移警告PHP

the*_*nix 176 php warnings

将我的php版本更新为5.4.0-3后,我收到一个奇怪的PHP错误.

我有这个数组:

Array
(
    [host] => 127.0.0.1
    [port] => 11211
)
Run Code Online (Sandbox Code Playgroud)

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

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


 Warning: Illegal string offset 'host' in ....
 Warning: Illegal string offset 'port' in ...
Run Code Online (Sandbox Code Playgroud)

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

Kzq*_*qai 269

该错误Illegal string offset 'whatever' in...通常意味着:您正在尝试将字符串用作完整数组.

这实际上是可行的,因为字符串可以被视为php中单个字符的数组.所以你认为$ var是一个带键的数组,但它只是一个带有标准数字键的字符串,例如:

$fruit_counts = array('apples'=>2, 'oranges'=>5, 'pears'=>0);
echo $fruit_counts['oranges']; // echoes 5
$fruit_counts = "an unexpected string assignment";
echo $fruit_counts['oranges']; // causes illegal string offset error
Run Code Online (Sandbox Code Playgroud)

你可以在这里看到这个:http: //ideone.com/fMhmkR

对于那些试图将错误的模糊性转化为关于它的事情的人来说,就像我一样.

  • 我打赌可以证明这是原始问题发生的原因.大多数评论错误地认为"未定义索引"是错误. (7认同)
  • 有时在尝试抓取多维数组的错误节点时可能会发生此错误,即“太深”,您需要选择的父节点 (2认同)

Jon*_*ell 74

TL; DR

你试图访问一个string好像它是一个数组,一个键是一个string.string我不明白.在代码中我们可以看到问题:

"hello"["hello"];
// PHP Warning:  Illegal string offset 'hello' in php shell code on line 1

"hello"[0];
// No errors.

array("hello" => "val")["hello"];
// No errors. This is *probably* what you wanted.
Run Code Online (Sandbox Code Playgroud)

深入

我们看看那个错误:

警告:非法字符串偏移'端口'在......

它说什么?它说我们正在尝试将字符串'port'用作字符串的偏移量.像这样:

$a_string = "string";

// This is ok:
echo $a_string[0]; // s
echo $a_string[1]; // t
echo $a_string[2]; // r
// ...

// !! Not good:
echo $a_string['port'];
// !! Warning: Illegal string offset 'port' in ...
Run Code Online (Sandbox Code Playgroud)

是什么导致这个?

出于某种原因,你期待一个array,但你有一个string.只是一个混乱.也许你的变量被改变了,也许它从来都不是array,它真的不重要.

可以做些什么?

如果我们知道我们应该有一个array,我们应该做一些基本的调试来确定我们为什么没有array.如果我们不知道我们是否会有,array或者string事情变得有点棘手.

我们可以做的是各种检查,以确保我们没有通知,警告或错误的事情,如is_array和,issetarray_key_exists:

$a_string = "string";
$an_array = array('port' => 'the_port');

if (is_array($a_string) && isset($a_string['port'])) {
    // No problem, we'll never get here.
    echo $a_string['port'];
}

if (is_array($an_array) && isset($an_array['port'])) {
    // Ok!
    echo $an_array['port']; // the_port
}

if (is_array($an_array) && isset($an_array['unset_key'])) {
    // No problem again, we won't enter.
    echo $an_array['unset_key'];
}


// Similar, but with array_key_exists
if (is_array($an_array) && array_key_exists('port', $an_array)) {
    // Ok!
    echo $an_array['port']; // the_port
}
Run Code Online (Sandbox Code Playgroud)

isset和之间有一些细微的差别array_key_exists.例如,如果值$array['key']null,isset返回false.array_key_exists只会检查一下,密钥是否存在.


小智 36

请尝试这种方式....我已经测试了这个代码....它的工作原理....

$memcachedConfig = array("host" => "127.0.0.1","port" => "11211");
print_r ($memcachedConfig['host']);
Run Code Online (Sandbox Code Playgroud)

  • 解释一下为什么这有效以及为什么OP的代码不起作用怎么样?像这样的答案是人们对 Stack Overflow 持怀疑态度的主要原因,尽管有很多人愿意解释答案中的每个细节,并实际上创建一些有用且有教育意义的内容。 (7认同)

Bri*_*ell 12

这里有很多很棒的答案 - 但我发现我的问题要简单得多.

我试图运行以下命令:

$x['name']   = $j['name'];
Run Code Online (Sandbox Code Playgroud)

我得到了这个illegal string错误,$x['name']因为我没有先定义数组.所以在尝试分配内容之前,我将以下代码行放入$x[]:

$x = array();
Run Code Online (Sandbox Code Playgroud)

它起作用了.


Mar*_*rco 6

这个问题有点晚了,但是对于其他正在搜索的人:我通过使用错误的值(类型)进行初始化而出现此错误:

$varName = '';
$varName["x"] = "test"; // causes: Illegal string offset
Run Code Online (Sandbox Code Playgroud)

正确的方法是:

 $varName = array();
 $varName["x"] = "test"; // works
Run Code Online (Sandbox Code Playgroud)


小智 5

从 PHP 5.4 开始,我们需要传递函数期望的相同数据类型值。例如:

function testimonial($id); // This function expects $id as an integer
Run Code Online (Sandbox Code Playgroud)

调用此函数时,如果提供了这样的字符串值:

$id = $array['id']; // $id is of string type
testimonial($id); // illegal offset warning
Run Code Online (Sandbox Code Playgroud)

由于数据类型不匹配,这将生成非法偏移警告。为了解决这个问题,您可以使用settype

$id = settype($array['id'],"integer"); // $id now contains an integer instead of a string
testimonial($id); // now running smoothly
Run Code Online (Sandbox Code Playgroud)