Tech Blog Using technology to help your business

14Jun/0665

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.

Comments (65) Trackbacks (1)
  1. Appreciate the recommendation. Let me try it out.

  2. I am also facing same issue. Any one can help me..
    Thanks

  3. 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 :(

  4. 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__));

  5. 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 :)

  6. Thanks for this article. I’ve been scratching my head wondering what on earth the error means, this helped make it clear!

  7. 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.

  8. 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?

  9. you can use if (is_array($foo)) { // go on with your code… }

  10. eca, from what I can gather through your comment, you have a problem with assigning arrays as previously described.

  11. 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

  12. 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

  13. 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.


Leave a comment

Plugin from the creators of Brindes :: More at Plulz Wordpress Plugins