Connecting Tech Pros Worldwide Forums | Help | Site Map

Cannot use string offset as an array

Member
 
Join Date: Nov 2007
Posts: 61
#1: Nov 4 '08
I get an error message like this.

Fatal error: Cannot use string offset as an array in D:\www\site\includes\change_preference.inc.php on line 310

[PHP]$cityName["$i"]["$j"] = $row2['city'];[/PHP]
The above code is causing this error. Please let me know what is wrong with this code. How can I rectify this error.

Here $i, $j consists of integer value. which are used to perform a for loop specific number of times. while $row2 brings data from a database.

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#2: Nov 4 '08

re: Cannot use string offset as an array


try $cityName's keys without the quotation marks. if that's not working use var_dump() to check for variable type and content.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#3: Nov 4 '08

re: Cannot use string offset as an array


Somewhere else you are declaring $cityName as a string and not a variable.

Post the full code using code tags.
Member
 
Join Date: Nov 2007
Posts: 61
#4: Nov 4 '08

re: Cannot use string offset as an array


I found which caused the error.
I have used a variable name similar to the array name $cityName that caused the confusion.

[PHP]$cityName = 'Dallas';
$cityName["$i"]["$j"] = $row2['city'];
[/PHP]
first I have used the $cityName as a string and then as an array which caused the error. I did not realize this because the above two lines were in different files and was included in another file. I think I should keep more appropriate names depending upon the file and context.
Member
 
Join Date: Nov 2007
Posts: 61
#5: Nov 4 '08

re: Cannot use string offset as an array


Quote:

Originally Posted by Markus

Somewhere else you are declaring $cityName as a string and not a variable.

Post the full code using code tags.

Oh! Thanks Markus. I did not see the post of yours. But anyhow I got it sorted out. great relief. Thank you very much!

and thank you too Dormilich
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,949
#6: Nov 4 '08

re: Cannot use string offset as an array


Quote:

Originally Posted by samatair

I found which caused the error.
I have used a variable name similar to the array name $cityName that caused the confusion.

[PHP]$cityName = 'Dallas';
$cityName["$i"]["$j"] = $row2['city'];
[/PHP]
first I have used the $cityName as a string and then as an array which caused the error. I did not realize this because the above two lines were in different files and was included in another file. I think I should keep more appropriate names depending upon the file and context.

As I said above. :)
Reply