Cannot use string offset as array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dabrowski
Forum Newbie
Posts: 3
Joined: Sat Nov 07, 2009 6:23 am

Cannot use string offset as array

Post by Dabrowski »

This morning my script that worked fine is giving me errors.

Err: Cannot use string offset as array

It seems that every time I use $_SESSION['item']['item2'] it gives me the error.

In the setup, I set:

Code: Select all

$_SESSION['user']['level'] = 9
Later in the script, anywhere I check:

Code: Select all

if ($_SESSION['user']['level'] == 9)
I get the error.

It seems to handle if I only use the top level. $_SESSION['user'] == 9
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Cannot use string offset as array

Post by Eric! »

Are you sure it is getting set? Try this right before the line that causes the error.

Code: Select all

echo '<pre>.'.print_r($_SESSION).'</pre>';
Dabrowski
Forum Newbie
Posts: 3
Joined: Sat Nov 07, 2009 6:23 am

Re: Cannot use string offset as array

Post by Dabrowski »

Yes, I did in a previously included file. If I check it in the same file I'm getting the error, it won't show since it fails.

I just tried a stripped down version in a single file and everything works, so now to show a live example would be problematic.

But yes, it was set.
Dabrowski
Forum Newbie
Posts: 3
Joined: Sat Nov 07, 2009 6:23 am

Re: Cannot use string offset as array

Post by Dabrowski »

I think I fixed it. I another file was unsetting $_SESSION.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Cannot use string offset as array

Post by Eric! »

Dabrowski wrote:Yes, I did in a previously included file. If I check it in the same file I'm getting the error, it won't show since it fails.
So how do you know it was set? I don't see any other problems, but we don't have access to your code either.

Edit: we posted at the same time...so it's set now
Post Reply