Henkka
Henkka

Reputation: 1663

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?

Upvotes: 102

Views: 190060

Answers (10)

Phil
Phil

Reputation: 164881

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.

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'];

PHP 7 tightened controls around 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.

Upvotes: 146

Kamil Dąbrowski
Kamil Dąbrowski

Reputation: 1092

My another bug with this problem was:

$arr = [];
$arr[] = 'add';
$arr = array_shift($arr); // <-- return array_shift is boolean not array ;)
//array_shift(array &$array): mixed
$arr[] = 'next'; // become error 

Upvotes: 0

Ganesh Ravikumar
Ganesh Ravikumar

Reputation: 1

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 '];
}

Upvotes: 0

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'];

Upvotes: 3

DrAnd1
DrAnd1

Reputation: 81

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.

Upvotes: 1

Darwin
Darwin

Reputation: 87

Solved!

$a['index'] = [];
$a['index'][] = 'another value';
$a['index'][] = 'another value';
$a['index'][] = 'another value';
$a['index'][] = 'another value';

Upvotes: 1

Maki
Maki

Reputation: 31

I had similar situation:

$foo = array();
$foo[] = 'test'; // error    
$foo[] = "test"; // working fine

Upvotes: 0

Jad Ayash
Jad Ayash

Reputation: 91

this was available in php 5.6 in php 7+ you should declare the array first

$users = array(); // not $users = ";
$users[] = "762";

Upvotes: 7

Alexander Cherkendov
Alexander Cherkendov

Reputation: 131

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);

Upvotes: 11

Shoe
Shoe

Reputation: 76260

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.

Upvotes: 55

Related Questions