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.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati
  • YahooMyWeb

17 Responses to “Fatal error: Cannot use string offset as an array in …”

  1. great stuff, really helped me out when getting this error with my session - thanks!

  2. thanks, to help me solve this problem…

  3. hi,

    I got the same fatal error on this line;-
    $eta_array = explode(”-”, $this->servicesTimeintransit[$type]["date"]);

    Do you know what is wrong with this line?
    i added $eta_array =array() before the line above still no difference

  4. Tough to diagnose with just that bit of info, but I wonder if your problem has to do with $this->servicesTimeintransit[$type][”date”]

    Does that matrix actually exist?

  5. Hi frank,

    Thanks. Actually its the error happens after PHP4 upgrade to PHP5. I think the matrix does exist as it work in PHP4 but not PHP5.

    How do I email you more on the code snippets?

  6. You can send me a message through the site.

  7. Hi Frank,

    “Fatal error: Cannot use string offset as an array”
    appears in the code. Happened on this line $eta_array = explode(”-”, $this->servicesTimeintransit[$type]["date"]);

    More code snippets below:-

    $methods = array();
    for ($i=0; $i servicesTimeintransit[$type])) {

    $eta_array = explode(”-”, $this->servicesTimeintransit[$type]["date"]);
    $months = array (” “, “January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”);
    $eta_arrival_date = $months[(int)$eta_array[1]].” “.$eta_array[2].”, “.$eta_array[0];
    $_type .= “, ETA: “.$eta_arrival_date;
    }
    // END of doing things differently:

    $methods[] = array(’id’ => $type, ‘title’ => $_type, ‘cost’ => ($this->handling_fee + $cost));
    }
    }

  8. CJJune

    The first thing that I see a problem with is your for loop statement.

    for ($i=0; $i servicesTimeintransit[$type])) {

    It should be in the form of

    for ($i=0; $i<$var; $i++) {

    I think you are getting “Fatal error: Cannot use string offset as an array” because servicesTimeintransit[$type] is an array and not an int or string. I gathered that servicesTimeintransit[$type] is an array as you are calling servicesTimeintransit[$type]["date"] two lines down.

    I hope that helps.

  9. Hi frank,

    I am not too sure how to solve this as this code is not written by me. This code is from UPSXML.

    So what should I replace the line of code with to eliminate error?

  10. Hmmm, if you did not write the code and have not changed anything, then you need to contact the creator of upsxml as the code that you have pasted is certainly incorrect in terms of the For loop.

    As for debugging the line $eta_array = explode(”-”, $this->servicesTimeintransit[$type][”date”]);

    try just returning the value to see what you are actually exploding by using

    echo($this->servicesTimeintransit[$type][”date”]);

  11. I forgot to answer your question. I don’t know what you should replace the For loop declaration with as I don’t know the proper logic the author was intending. The current declaration posted

    for ($i=0; $i servicesTimeintransit[$type])) {

    does not give me any indication.

    I think someone has made a change to your code and you should find a backup copy to compare what has been change. Then move forward in debugging any further errors.

    I hope that helps.

  12. :-P…

    I have no idea how?
    Could be Curl problem in PHP5 as it works in PHP4?

  13. You would have errors pointing to Curl if that was the issue.

  14. Yeap….I guess its not CURL as the error is on the line of code pertaining to array. …:-(

  15. thank you for explanation ;)

  16. Hi frank

    something weird happened on my website ( weird for me at least **blush****

    my website has been online for a year now.
    the, 2 days ago , for no reason, when someone makes a search on my website, it gives :

    Fatal error: Cannot use string offset as an array in /home/ramzi76/public_html/smartway/lib/system.lib on line 192

    first. why such errors are auto-generated? i mean no one touched the files or updated anything?

    now when i go to the said line, it is :
    return strtoupper($fMemb[ login ][ 0 ]).str_repeat(”0″, 6 - strlen($fMemb[ id ])).$fMemb[ id ];
    } // sysGetProfileCode

    where is the error?

    thx

  17. ramzi,

    Something must have if it was working before and now you are getting an error. Changing from one version of php to another might have different results in how implicit declarations happen also. I am not sure how each version handles it as I have not done full research into that, but I’m sure you can Google it.

    One thing you can do debug your problem is to make sure
    $fMemb[ login ][ 0 ]
    $fMemb[ id ]
    are both set and are also strings. It is hard to tell from just looking at the code as we cannot see what those array elements hold at the time of the error. If you can post the echos of those array elements to show the strings values, then it would help in determining the problem of you situation.

Discussion Area - Leave a Comment