Problems with arrays

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
nyl
Forum Newbie
Posts: 1
Joined: Fri Jun 03, 2005 9:19 pm

Problems with arrays

Post by nyl »

I have this code

Code: Select all

$_SESSION['productid'][] = $temp_productid ;
When I load the page the first time it works.
After that it gives me this error:

Code: Select all

Fatal error: [] operator not supported for strings....
It works fine on my machine locally. when I upolad
the file to the server it gives me this problem.

Someone said it happens when a string is assigned to an array that makes the problem. But it works on my computer.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Before the error line what does print_r($_SESSION) give ?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Re: Problems with arrays

Post by neophyte »

nyl wrote:I have this code

Code: Select all

$_SESSION['productid'][] = $temp_productid ;
When I load the page the first time it works.
After that it gives me this error:

Code: Select all

Fatal error: [] operator not supported for strings....
First always code with error_reporting(E_ALL); at the top of your page. This will give you consistant error reporting and more stable code. Then remove it when you done testing. I'd print $_SESSION array to the screen. If the problem persists I'd a assign a key to 'productid'.

Code: Select all

$_SESSION['productid']['some_num_or_name_val'] = $temp_productid;
If the idea is to have multiple product ids assigned to the value make them seperated with a "," or "|" and explode()/implode() the list as needed...

Hope this helps...
Post Reply