I-AM-OBODO Posted June 4, 2019 Share Posted June 4, 2019 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 */ } Quote Link to comment https://forums.phpfreaks.com/topic/308793-illegal-string-offset-user_id/ Share on other sites More sharing options...
Barand Posted June 4, 2019 Share Posted June 4, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/308793-illegal-string-offset-user_id/#findComment-1567303 Share on other sites More sharing options...
I-AM-OBODO Posted June 4, 2019 Author Share Posted June 4, 2019 (edited) 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 June 4, 2019 by I-AM-OBODO Quote Link to comment https://forums.phpfreaks.com/topic/308793-illegal-string-offset-user_id/#findComment-1567304 Share on other sites More sharing options...
Barand Posted June 4, 2019 Share Posted June 4, 2019 11 minutes ago, Barand said: $value is not an array, specifically not an array with a key of 'user_id' 1 hour ago, I-AM-OBODO said: /* echo $value //works */ It works because it is the correct code. Quote Link to comment https://forums.phpfreaks.com/topic/308793-illegal-string-offset-user_id/#findComment-1567305 Share on other sites More sharing options...
Barand Posted June 4, 2019 Share Posted June 4, 2019 P.S. If you just want the user_name echo $_SESSION['test']['user_name']; Quote Link to comment https://forums.phpfreaks.com/topic/308793-illegal-string-offset-user_id/#findComment-1567306 Share on other sites More sharing options...
I-AM-OBODO Posted June 4, 2019 Author Share Posted June 4, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/308793-illegal-string-offset-user_id/#findComment-1567307 Share on other sites More sharing options...
Barand Posted June 4, 2019 Share Posted June 4, 2019 Try this Quote Link to comment https://forums.phpfreaks.com/topic/308793-illegal-string-offset-user_id/#findComment-1567308 Share on other sites More sharing options...
I-AM-OBODO Posted June 4, 2019 Author Share Posted June 4, 2019 5 minutes ago, Barand said: Try this thanks Quote Link to comment https://forums.phpfreaks.com/topic/308793-illegal-string-offset-user_id/#findComment-1567309 Share on other sites More sharing options...
cyberRobot Posted June 4, 2019 Share Posted June 4, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/308793-illegal-string-offset-user_id/#findComment-1567313 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.