Fatal error: Cannot use string offset as an array in …
PHP5 Error message that is caused by attempting to assign a value to an array element of a variable that is declared as a string.
Example that generates error:
$foo='bar';
$foo[0]='bar';
Get error message Fatal error: Cannot use string offset as an array in ...
Explanation
$foo was declared as a string in $foo='bar'.
$foo[0] is trying to append an element onto a string variable.
Example that does not generate error:
$foo[0]='bar';
$foo='bar';
Does NOT generate error.
Explanation
$foo[0]='bar' instantiates variable $foo as array since it has not been instantiated. Then assigns 'bar' to element $foo[0].
$foo='bar' implicitly re-declares $foo as a string and assigns 'bar' to it.
Example that does not generate error:
$foo='bar';
$foo=array();
$foo[0]='bar';
Explanation
$foo='bar' implicitly declares $foo as a string variable then assigns 'bar' as the value.
$foo=array() explicitly re-declares $foo as an array.
$foo[0]='bar' can now be executed as $foo is declared as an array.
Let me know if this helped you or if I am not clear on anything. Thanks.
January 21st, 2012 - 16:22
I am very confused right now. What do I need to do to get rid of this error? I can't access my admin panel now cuz of this error
November 14th, 2011 - 16:02
great explanation, thanks!
September 13th, 2011 - 08:47
A quick and dirty for for WordPress users is to just redeclare the element as an array prior to adding array element values.
Example:
$cformsSettings = array(); // redeclate this variable as an array
$cformsSettings['global']['plugindir'] = basename(dirname(__FILE__));
July 27th, 2011 - 07:55
Many thanks!!!
I had a problem with an upgrade of my Forum and I solved it in 1 minute with your great tip.
Thanks again
March 4th, 2011 - 03:29
Thanks for this article. I’ve been scratching my head wondering what on earth the error means, this helped make it clear!
December 10th, 2010 - 10:20
James, sounds like you are probably accessing the deep element incorrectly. Best thing to do is do print_r on your array to see if the structure is what you think it is.
December 10th, 2010 - 08:48
I have an associative array that i am assigning to a new element. Then i am using the deeper key of the associative element of the newly assigned element and i am getting this error. How can i solve it?
December 1st, 2010 - 13:46
you can use if (is_array($foo)) { // go on with your code… }
June 1st, 2010 - 12:59
eca, from what I can gather through your comment, you have a problem with assigning arrays as previously described.
May 31st, 2010 - 02:29
please help me about 6this error…
Fatal error: Cannot use string offset as an array in /home/h25380/public_html/wp-content/plugins/wp-simpleviewer/wp-simpleviewer-admin.php on line 992
April 27th, 2010 - 06:27
And here it is the difference between a beginner and a professional
Thanks a lot frank!
SQL is starting to be more than SELECT, FROM, ORDER BY and WHERE for me… xd
April 26th, 2010 - 11:25
Meee, instead of trying to solve the complicated code you are faced with, maybe there is a better way to accomplish your requirements. If you want to get the total count of each unique combination of country and city, do the following query to the database.
SELECT country, city, count(*) as total FROM yourtable GROUP BY country, city;
Let me know if I have missed what you are trying to do.