php 问题:致命错误:[] 运算符不支持中的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5879675/
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
Problem with: Fatal error: [] operator not supported for strings in
提问by Henkka
I'm getting information from database, saving it in array and echoing it in a form with loop structure and I'm having problems when I try to save the modified information to database.
我从数据库中获取信息,将其保存在数组中并以具有循环结构的形式回显它,当我尝试将修改后的信息保存到数据库时遇到了问题。
I'm getting this error:
我收到此错误:
Fatal error: [] operator not supported for strings in....
致命错误:[] 运算符不支持...中的字符串。
Code:
代码:
$namesql1 = "SELECT name,date,text,date2 FROM table WHERE something= '$something'";
$nameresult1 = mysql_query($namesql1);
$countrows = mysql_num_rows($nameresult1);
while ($row = mysql_fetch_array($nameresult1, MYSQL_ASSOC)) {
$name[] = $row['name'];
$date[] = $row['date'];
$text[] = $row['text'];
$date2[] = $row['date2 '];
}
/** SOME CODE HERE **/
$wrotesql = "UPDATE service_report SET name ='$name' , $date = '$date',$text = '$text[$nro]', ser_date = '$date2[$nro]' WHERE something = '$something')";
$wroteresult = mysql_query($wrotesql);
Could somebody please give me a hint what I'm doing wrong?
有人可以给我一个提示我做错了什么吗?
Thank you.
谢谢你。
回答by Phil
You get this error when attempting to use the short array push syntax on a string.
尝试对字符串使用短数组推送语法时会出现此错误。
For example, this
例如,这
$foo = 'foo';
$foo[] = 'bar'; // ERROR!
I'd hazard a guess that one or more of your $name
, $date
, $text
or $date2
variables has been initialised as a string.
我大胆猜测的一个或多个你的$name
,$date
,$text
或$date2
变量已经被初始化为一个字符串。
Edit:Looking again at your question, it looks like you don't actually want to use them as arrays as you're treating them as strings further down.
编辑:再次查看您的问题,看起来您实际上并不想将它们用作数组,因为您将它们进一步视为字符串。
If so, change your assignments to
如果是这样,请将您的分配更改为
$name = $row['name'];
$date = $row['date'];
$text = $row['text'];
$date2 = $row['date2'];
It seems there are some issues with PHP 7 and code using the empty-indexarray push syntax.
PHP 7 和使用空索引数组推送语法的代码似乎存在一些问题。
To make it clear, these work finein PHP 7+
明确地说,这些在 PHP 7+ 中工作正常
$previouslyUndeclaredVariableName[] = 'value'; // creates an array and adds one entry
$emptyArray = []; // creates an array
$emptyArray[] = 'value'; // pushes in an entry
What does not workis attempting to use empty-indexpush on any variable declared as a string, number, object, etc, ie
什么是不工作正在尝试使用空指数上宣布为字符串,数字,对象等,即任何变量推
$declaredAsString = '';
$declaredAsString[] = 'value';
$declaredAsNumber = 1;
$declaredAsNumber[] = 'value';
$declaredAsObject = new stdclass();
$declaredAsObject[] = 'value';
All result in a fatal error.
所有这些都会导致致命错误。
回答by Shoe
You have probably defined $name
, $date
, $text
or $date2
to be a string, like:
你可能已经定义$name
,$date
,$text
或者$date2
是一个字符串,如:
$name = 'String';
Then if you treat it like an array it will give that fatal error:
然后,如果您将其视为数组,则会出现致命错误:
$name[] = 'new value'; // fatal error
To solve your problem just add the following code at the beginning of the loop:
要解决您的问题,只需在循环的开头添加以下代码:
$name = array();
$date = array();
$text = array();
$date2 = array();
This will reset their value to array and then you'll able to use them as arrays.
这会将它们的值重置为数组,然后您就可以将它们用作数组。
回答by Alexander Cherkendov
Such behavior is described in Migrating from PHP 7.0.x to PHP 7.1.x/
从 PHP 7.0.x 迁移到 PHP 7.1.x/ 中描述了此类行为
The empty index operator is not supported for strings anymoreApplying the empty index operator to a string (e.g. $str[] = $x) throws a fatal error instead of converting silently to array.
字符串不再支持空索引运算符 将空索引运算符应用于字符串(例如 $str[] = $x)会引发致命错误,而不是静默转换为数组。
In my caseit was a mere initialization. I fixed it by replacing $foo=''
with $foo=[]
.
就我而言,这只是一个初始化。我通过替换$foo=''
为$foo=[]
.
$foo='';
$foo[]='test';
print_r($foo);
回答by Jad Ayash
this was available in php 5.6 in php 7+ you should declare the array first
这在 php 7+ 的 php 5.6 中可用,您应该先声明数组
$users = array(); // not $users = ";
$users[] = "762";
回答by Maki
I had similar situation:
我有类似的情况:
$foo = array();
$foo[] = 'test'; // error
$foo[] = "test"; // working fine