非法字符串偏移警告PHP
在将PHP版本更新到5.4.0-3之后,我遇到了一个奇怪的PHP错误 我有这个阵列:非法字符串偏移警告PHP,php,warnings,Php,Warnings,在将PHP版本更新到5.4.0-3之后,我遇到了一个奇怪的PHP错误 我有这个阵列: Array ( [host] => 127.0.0.1 [port] => 11211 ) 当我尝试这样访问它时,会收到奇怪的警告 print $memcachedConfig['host']; print $memcachedConfig['port']; Warning: Illegal string offset 'host' in .... Warning: I
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("host" => "127.0.0.1","port" => "11211");
print_r($memcachedConfig['host']);
在…中出现的错误
非法字符串偏移量“whatever”通常意味着:您试图将字符串用作完整数组
这实际上是可能的,因为字符串在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
function testimonial($id); // This function expects $id as an integer
您可以在此处看到这一点:
对于那些试图将模糊的错误转化为解决问题的人,就像我一样。在检查数组之前,请执行以下操作:
if(!is_array($memcachedConfig))
$memcachedConfig = array();
您正试图访问一个字符串
,就像它是一个数组一样,使用的键是字符串
<代码>字符串
无法理解这一点。在代码中,我们可以看到问题:
"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.
深入 让我们看看这个错误: 警告:在 上面说什么?它说我们试图使用字符串
'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 ...
这是什么原因?
出于某种原因,您需要一个数组
,但您有一个字符串
。只是搞错了。也许你的变量被改变了,也许它从来不是一个数组,这真的不重要
可以做些什么?
如果我们知道应该有一个数组
,我们应该做一些基本的调试来确定为什么没有数组
。如果我们不知道是否会有一个数组
或字符串
,事情就会变得有点棘手
我们可以做的是进行各种检查,以确保我们没有关于和或的通知、警告或错误:
和之间有一些微妙的区别。例如,如果$array['key']
的值为null
,isset
返回false
array\u key\u exists
将只检查key是否存在在我的例子中,我将mysql\u fetch\u assoc更改为mysql\u fetch\u array并求解。解决这个问题需要3天时间:-(我的项目的其他版本使用fetch assoc运行。这里有很多很好的答案,但我发现我的问题要简单得多
我试图运行以下命令:
$x['name'] = $j['name'];
我在$x['name']
上遇到了一个非法字符串
错误,因为我没有首先定义数组。因此,在尝试将内容分配给$x[]
之前,我输入了以下代码行:
$x = array();
从PHP 5.4开始,我们需要传递函数所需的相同数据类型值。例如:
$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
function testimonial($id); // This function expects $id as an integer
调用此函数时,如果提供如下字符串值:
$id = $array['id']; // $id is of string type
testimonial($id); // illegal offset warning
由于数据类型不匹配,这将生成非法偏移警告。为了解决此问题,您可以使用:
以防万一,我遇到这个错误是因为我忘了取消序列化数组。我肯定会检查它是否适用于您的案例。这是一个旧的案例,但万一有人可以从中受益。
如果数组为空,也会出现此错误
就我而言,我有:
$buyers_array = array();
$buyers_array = tep_get_buyers_info($this_buyer_id); // returns an array
...
echo $buyers_array['firstname'] . ' ' . $buyers_array['lastname'];
我改为:
$buyers_array = array();
$buyers_array = tep_get_buyers_info($this_buyer_id); // returns an array
...
if(is_array($buyers_array)) {
echo $buyers_array['firstname'] . ' ' . $buyers_array['lastname'];
} else {
echo 'Buyers id ' . $this_buyer_id . ' not found';
}
在我的例子中,当我改变执行sql查询的函数时,我解决了这个问题
之后:返回json\u encode($array)
然后:return$array
回答这个问题有点晚,但是对于正在搜索的其他人来说:我是通过使用错误的值(键入)初始化而得到这个错误的:
正确的方法是:
$varName = array();
$varName["x"] = "test"; // works
这对我很有用:
矿山测试规范:
$var2['data'] = array ('a'=>'21','b'=>'32','c'=>'55','d'=>'66','e'=>'77');
foreach($var2 as $result)
{
$test = $result['c'];
}
print_r($test);
输出:55
伙计们,请检查一下。谢谢你用一下
$memcachedConfig = array();
以前
print $memcachedConfig['host'];
print $memcachedConfig['port'];
Warning: Illegal string offset 'host' in ....
Warning: Illegal string offset 'port' in ....
这是因为您从未定义什么是$memcachedConfig,因此默认情况下将按字符串而不是数组处理。我使用trim()函数解决了这个问题。问题在于空间
所以让我们试试看
$unit_size = []; //please declare the variable type
$unit_size = exolode("x", $unit_size);
$width = trim ($unit_size[1] );
$height = trim ($unit_size[2] );
我希望这会对您有所帮助。我认为这条消息的唯一原因是因为目标数组实际上是一个类似字符串etc(JSON->{“host”:“127.0.0.1”})的数组变量for 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的,因此请了解如何在其他语言中检查数组是否为空。显然$memcachedConfig
不是该数组。Showvar\u dump($memcachedConfig);
这意味着键不存在。在“print”之前用var\u export($memcachedConfig)
检查变量大多数人忽略的是,这并不意味着索引不存在——这会产生“未定义的索引”消息。这是一个不同的错误。这里是正确的答案。注意,任何查看此问题的人:此问题的正确答案不是标记的答案;正确答案是Kzqai的下面找到的。感谢您的帮助。var_dump help。我从配置文件加载了数组,该文件的strage内容如下。array(2){[“host”]=>字符串(9)“127.0.0.1”[“端口”]=>字符串(5)“11211”}字符串(5)“m_prefix”PHP5.4现在$xx['host']正确抛出了警告。在包含_once($file);
之后,我也出现了相同的错误。数组已经正确构建(调试信息显示了这一点)但是,在没有PHP非法stringoffset警告消息的情况下,它必须手动复制到另一个数组中才能使用。$sStartDate=date(“Y-m-d”,strtotime($feed['DTSTART']['value']);$sEndDate=date(“Y-m-d”,strtotime($feed['DTEND']['value']));如何修复此处的同一错误警告:非法字符串偏移如何解释此操作为何有效以及OP的代码为何无效?这些答案是人们怀疑堆栈溢出的主要原因,尽管有很多人怀疑堆栈溢出
//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']);
}