非法字符串偏移警告 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9869150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Illegal string offset Warning PHP
提问by thesonix
I get a strange PHP error after updating my php version to 5.4.0-3.
将我的 php 版本更新到 5.4.0-3 后,我收到一个奇怪的 PHP 错误。
I have this array:
我有这个数组:
Array
(
[host] => 127.0.0.1
[port] => 11211
)
When I try to access it like this I get strange warnings
当我尝试像这样访问它时,我收到奇怪的警告
print $memcachedConfig['host'];
print $memcachedConfig['port'];
Warning: Illegal string offset 'host' in ....
Warning: Illegal string offset 'port' in ...
I really don't want to just edit my php.ini and re-set the error level.
我真的不想只是编辑我的 php.ini 并重新设置错误级别。
采纳答案by letsnurture
Please try this way.... I have tested this code.... It works....
请尝试这种方式......我已经测试了这段代码......它有效......
$memcachedConfig = array("host" => "127.0.0.1","port" => "11211");
print_r($memcachedConfig['host']);
回答by Kzqai
The error Illegal string offset 'whatever' in...
generally means: you're trying to use a string as a full array.
该错误Illegal string offset 'whatever' in...
通常意味着:您试图将字符串用作完整数组。
That is actually possible since strings are able to be treated as arrays of single characters in php. So you're thinking the $var is an array with a key, but it's just a stringwith standard numeric keys, for example:
这实际上是可能的,因为字符串可以被视为 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
You can see this in action here: http://ideone.com/fMhmkR
你可以在这里看到这个:http: //ideone.com/fMhmkR
For those who come to this question trying to translate the vagueness of the error into something to do about it, as I was.
对于那些试图将错误的模糊性转化为对它做些什么的人来回答这个问题,就像我一样。
回答by Jon Surrell
TL;DR
TL; 博士
You're trying to access a string
as if it were an array, with a key that's a string
. string
will not understand that. In code we can see the problem:
您试图访问 astring
就好像它是一个数组一样,键是 a 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.
In depth
深入
Let's see that error:
让我们看看这个错误:
Warning: Illegal string offset 'port' in ...
警告:非法字符串偏移量'port' in ...
What does it say? It says we're trying to use the string 'port'
as an offset for a string. Like this:
它说什么?它说我们正在尝试使用字符串'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 ...
What causes this?
这是什么原因造成的?
For some reason you expected an array
, but you have a string
. Just a mix-up. Maybe your variable was changed, maybe it never was an array
, it's really not important.
出于某种原因,您期望有一个array
,但您有一个string
. 只是混搭而已。也许你的变量被改变了,也许它从来都不是array
,这真的不重要。
What can be done?
可以做什么?
If we knowwe should have an array
, we should do some basic debugging to determine why we don't have an array
. If we don't know if we'll have an array
or string
, things become a bit trickier.
如果我们知道我们应该有一个array
,我们应该做一些基本的调试来确定为什么我们没有array
. 如果我们不知道我们是否会有一个array
or string
,事情就会变得有点棘手。
What we cando is all sorts of checking to ensure we don't have notices, warnings or errors with things like is_array
and isset
or array_key_exists
:
我们可以做的是各种检查,以确保我们没有类似is_array
和isset
或这样的通知、警告或错误array_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
}
There are some subtle differences between isset
and array_key_exists
. For example, if the value of $array['key']
is null
, isset
returns false
. array_key_exists
will just check that, well, the key exists.
isset
和之间有一些细微的差别array_key_exists
。例如,如果值$array['key']
是null
,isset
返回false
。array_key_exists
将只检查密钥是否存在。
回答by Brian Powell
There are a lot of great answers here - but I found my issue was quite a bit more simple.
这里有很多很棒的答案 - 但我发现我的问题要简单得多。
I was trying to run the following command:
我试图运行以下命令:
$x['name'] = $j['name'];
and I was getting this illegal string
error on $x['name']
because I hadn't defined the array first. So I put the following line of code in before trying to assign things to $x[]
:
我收到这个illegal string
错误是$x['name']
因为我没有先定义数组。因此,在尝试将内容分配给之前,我将以下代码行放入$x[]
:
$x = array();
and it worked.
它奏效了。
回答by Marco
A little bit late to the question, but for others who are searching: I got this error by initializing with a wrong value (type):
这个问题有点晚了,但对于正在搜索的其他人:我通过使用错误的值(类型)进行初始化得到了这个错误:
$varName = '';
$varName["x"] = "test"; // causes: Illegal string offset
The right way is:
正确的方法是:
$varName = array();
$varName["x"] = "test"; // works
回答by Anirudh Sood
As from PHP 5.4 we need to pass the same datatype value that a function expects. For example:
从 PHP 5.4 开始,我们需要传递函数期望的相同数据类型值。例如:
function testimonial($id); // This function expects $id as an integer
When invoking this function, if a string value is provided like this:
调用此函数时,如果提供了这样的字符串值:
$id = $array['id']; // $id is of string type
testimonial($id); // illegal offset warning
This will generate an illegal offset warning because of datatype mismatch. In order to solve this, you can use settype
:
由于数据类型不匹配,这将生成非法偏移警告。为了解决这个问题,您可以使用settype
:
$id = settype($array['id'],"integer"); // $id now contains an integer instead of a string
testimonial($id); // now running smoothly
回答by dlopezgonzalez
Before to check the array, do this:
在检查数组之前,请执行以下操作:
if(!is_array($memcachedConfig))
$memcachedConfig = array();
回答by Y. Joy Ch. Singha
It works to me:
它对我有用:
Testing Code of mine:
我的测试代码:
$var2['data'] = array ('a'=>'21','b'=>'32','c'=>'55','d'=>'66','e'=>'77');
foreach($var2 as $result)
{
$test = $result['c'];
}
print_r($test);
Output: 55
输出: 55
Check it guys. Thanks
检查一下伙计们。谢谢
回答by chayankQ
just use
只是使用
$memcachedConfig = array();
before
前
print $memcachedConfig['host'];
print $memcachedConfig['port'];
Warning: Illegal string offset 'host' in ....
Warning: Illegal string offset 'port' in ....
this is because you never define what is $memcachedConfig, so by default are treated by string not arrays..
这是因为你从来没有定义什么是 $memcachedConfig,所以默认情况下是由字符串而不是数组来处理的。
回答by Jailendra Rajawat
I solved this problem by using trim() function. the issue was with space.
我通过使用 trim() 函数解决了这个问题。问题在于空间。
so lets try
所以让我们试试
$unit_size = []; //please declare the variable type
$unit_size = exolode("x", $unit_size);
$width = trim ($unit_size[1] );
$height = trim ($unit_size[2] );
I hope this will help you.
我希望这能帮到您。