Jump to content

Illegal string offset 'user_id'


I-AM-OBODO

Recommended Posts

Hi guys, why am i getting this error: Illegal string offset 'user_id' 

but when echo $value it brings the correct output.

Thanks

	    $user_id = 5;
    $user_name = "obodo";
    $_SESSION['test'] = array('user_id' => $user_id, 'user_name' => $user_name);
	
    foreach( $_SESSION['test'] as $value )
    {
         
	         echo $value['user_id'];   //give error
        /* echo $value //works  */
      
    }
     

Link to comment
Share on other sites

The usual advice I would give for this problem would be for you to echo $value within the loop so you can see what the variable contains.

In your case you have already done this and you will have seen the values "5" and "obodo".

As it is then blatantly obvious that $value is not an array, specifically not an array with a key of 'user_id', then it's hard to see why you even need to ask the question.

Link to comment
Share on other sites

6 minutes ago, Barand said:

The usual advice I would give for this problem would be for you to echo $value within the loop so you can see what the variable contains.

In your case you have already done this and you will have seen the values "5" and "obodo".

As it is then blatantly obvious that $value is not an array, specifically not an array with a key of 'user_id', then it's hard to see why you even need to ask the question.

I dont get you? for instance now, i want value of only the user_name and instead of outputting "obodo" it gives error. and you said $value is not an array?

Edited by I-AM-OBODO
Link to comment
Share on other sites

1 minute ago, Barand said:

P.S.

If you just want the user_name


echo $_SESSION['test']['user_name'];

 

yeah i know. but the problem is that i want to get all the values from a form, then list them just like a shopping cart where u find all the added items before continue shopping or checkouts. i guess its a multi-dimensional array. maybe am wrong though.

thanks

Link to comment
Share on other sites

Perhaps you've already figured this out using Barand's advice. Your use of the foreach loop only goes through the values of the array. It doesn't matter if you are using an indexed array (uses numbers for the keys) or an associative array (uses strings for the keys).

If you need to access the keys from the array, you can modify your foreach loop as follows:

foreach( $_SESSION['test'] as $key => $value )

Note that $value['user_id'] still won't work. You'll need to use $value to access the value and $key to access the key. More information, including examples, can be found here: https://www.php.net/manual/en/control-structures.foreach.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.