'Fatal error: [] operator not supported for strings
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?
Solution 1:[1]
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.
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-index array push syntax.
To make it clear, these work fine in PHP 7+
$previouslyUndeclaredVariableName[] = 'value'; // creates an array and adds one entry
$emptyArray = []; // creates an array
$emptyArray[] = 'value'; // pushes in an entry
What does not work is attempting to use empty-index push 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.
Solution 2:[2]
You have probably defined $name
, $date
, $text
or $date2
to be a string, like:
$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.
Solution 3:[3]
Such behavior is described in Migrating from PHP 7.0.x to PHP 7.1.x/
The empty index operator is not supported for strings anymore Applying the empty index operator to a string (e.g. $str[] = $x) throws a fatal error instead of converting silently to array.
In my case it was a mere initialization. I fixed it by replacing $foo=''
with $foo=[]
.
$foo='';
$foo[]='test';
print_r($foo);
Solution 4:[4]
this was available in php 5.6 in php 7+ you should declare the array first
$users = array(); // not $users = ";
$users[] = "762";
Solution 5:[5]
Solved!
$a['index'] = [];
$a['index'][] = 'another value';
$a['index'][] = 'another value';
$a['index'][] = 'another value';
$a['index'][] = 'another value';
Solution 6:[6]
I agree with Jeremy Young's comment on Phils answer:
I have found that this can be a problem associated with migrating from php 5 to php 7. php 5 was more tolerant of amibiguity in whether a variable was an array or not than php 7 is. In most cases the solution is to declare the array explicitly, as explained in this answer.
I was just trouble shooting a Wordpress plugin after the migration of php5 to php7. Since the plugin code was relying on user input, and it was intrinsically used in the code either as string, or as array, I added the following code in to prevent a fatal error:
if(is_array($variable_either_string_or_array)){
// if it's an array, declaration is allowed:
$variable_either_string_or_array[]=$additionalInfoData[$i];
}else{
// if it's not an array, declaration it as follows:
$variable_either_string_or_array=$additionalInfoData[$i];
}
This was the only modification I needed to add to make the plugin php7-proof. Obviously not "best practices", I'd rather read and understand the full code.. but a quick fix was needed.
Solution 7:[7]
I was getting the same error when declaring a variable as a string and then writing it as an array. This is how it works without error
$name = array();
$name[] = $row['name'];
Solution 8:[8]
I had similar situation:
$foo = array();
$foo[] = 'test'; // error
$foo[] = "test"; // working fine
Solution 9:[9]
Coding this way will fix the error
$name=array();
$date=array();
$text=array();
$date2=array();
while ($row = mysql_fetch_array($nameresult1, MYSQL_ASSOC)) {
$name[] = $row['name'];
$date[] = $row['date'];
$text[] = $row['text'];
$date2[] = $row['date2 '];
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Shoe |
Solution 3 | |
Solution 4 | Jad Ayash |
Solution 5 | Darwin |
Solution 6 | DrAnd1 |
Solution 7 | Or Assayag |
Solution 8 | Maki |
Solution 9 | Ganesh Ravikumar |