[英]Cannot use string offset as an array breaks in php5.2, not in php5.4
I've got this problem with php, and I guess the worst part is that it's working fine on my dev environment (php v5.4) but breaks on the test/live site on the webserver (php v5.2). 我用php遇到了这个问题,我猜最糟糕的是它在我的开发环境(php v5.4)上工作正常,但在网络服务器(php v5.2)上的测试/实时网站上中断了。
So when I var_dump my $_POST["formData"] I get an array that looks like this: 所以当我var_dump我的$ _POST [“formData”]时,我得到一个如下所示的数组:
array(42) {
[0] => array(2) {
["name"] => string(2) "id";
["value"] => string(4) "3972";
}
[1] => array(2) {
["name"] => string(2) "action";
["value"] => string(4) "edit";
}
...
}
To separate this, I use this (or some variation of this): 要分开这个,我使用它(或其中的一些变体):
for($i=0;$i<count($_POST["formData"]);$i++) {
$data[$_POST["formData"][$i]["name"]] = $_POST["formData"][$i]["value"];
}
So I end up being able to access everything with: 所以我最终能够通过以下方式访问所有内容:
foreach($data as $key => $value) {
echo $key . " = " . $value . "<br />";
}
which outputs: 哪个输出:
id = 3972
action = edit
...
So, again, this works just fine on my dev server, but breaks on the live site. 所以,再一次,这在我的开发服务器上运行得很好,但在现场网站上中断了。 I've looked around here and found a lot of examples, but it seems a lot of them are using examples that aren't working quite the same.
我在这里看了一下,发现了很多例子,但似乎很多人都在使用不完全相同的例子。
What's causing this? 是什么导致了这个? Is it some setting?
这是一些设置吗? Is it a change between the two versions?
这是两个版本之间的变化吗? I've tried a couple other things but none of them have worked and it's kinda a pain testing on the testing server (uploading files one at a time...yay...).
我尝试过其他一些东西,但是没有一个能够工作,这对测试服务器进行了一次痛苦的测试(一次上传一个文件......是的......)。 Any easy solution or do I have to rebuild my script?
任何简单的解决方案或我必须重建我的脚本?
That error suggests you're using a string as if it were an array. 该错误表明您正在使用字符串,就好像它是一个数组一样。 I'll assume it's your
for
loop. 我会假设这是你
for
循环。 I'm not entirely sure, but $data
could be being implicitly initialised to an empty string, which you are then trying to dereference and assign to. 我不完全确定,但
$data
可能被隐式初始化为空字符串,然后您尝试取消引用并分配给它。 Does rewriting it like this help? 是否重写它有帮助?
$data = array();
foreach($_POST['formData'] as $kvp) {
$data[$kvp['name']] = $kvp['value'];
}
This code is also faster, because you're not calling count()
in a loop. 这段代码也更快,因为你没有在循环中调用
count()
。
It turns out this is indeed a difference between PHP versions, as demonstrated by this handy comparison tool . 事实证明,这确实是PHP版本之间的差异,正如这个方便的比较工具所证明的那样 。
For reference, the somewhat confusing error comes about (prior to PHP 5.4) from the following two steps: 作为参考,从以下两个步骤出现了一些有点令人困惑的错误(在PHP 5.4之前):
$foo[1]
or $foo['bar']
. $foo[1]
或$foo['bar']
。 PHP takes this to mean "get the Nth character of the string" - $foo[1]
means the 2nd character of $foo
, and $foo['bar']
means the first, because the bar
gets converted to 0
. $foo[1]
指的第2个字符$foo
和$foo['bar']
是指第一个,因为bar
被转换为0
。 So in some situations, your $_POST["formData"]
is not an array, but a string. 因此在某些情况下,
$_POST["formData"]
不是数组,而是字符串。 This is probably happening sometimes on both environments, but the difference in error handling is masking it in one environment and not the other. 这可能有时在两种环境中都会发生,但错误处理的不同之处在于在一个环境中屏蔽它而不是在另一个环境中屏蔽它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.