barkster Posted September 8, 2006 Share Posted September 8, 2006 How do I put a array into a session variable?? I have a text field that is stored in as a comman seperated values and I need to dump it into a session and then acess the session like print $_SESSION['Names'][0] I tried this but it doesn't work.$_SESSION['Names'] = array(split(",",$_POST['names']));I was trying to access at $_SESSION['Names'][0] but I get "array" Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 8, 2006 Share Posted September 8, 2006 Do a [code]<?phpecho '<pre>' . print_r($_SESSION['Names'],true) . '</pre>';?>[/code]to see what you put into that session variable. Then you should be able to figure out how to retrieve it.Ken Quote Link to comment Share on other sites More sharing options...
jefkin Posted September 8, 2006 Share Posted September 8, 2006 Maybe a better solution is to store your array data with serailize()/unserialize()saver:$_SESSION['names'] = serialize($names);....reader:$names = unserialize($_SESSION['names']);....HTHJeff Quote Link to comment Share on other sites More sharing options...
Barand Posted September 8, 2006 Share Posted September 8, 2006 Don't forget to put session_start();at top of script if you are going to be using $_SESSION Quote Link to comment Share on other sites More sharing options...
Barand Posted September 8, 2006 Share Posted September 8, 2006 PS$_SESSION['Names'] = explode(",",$_POST['names']); Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 8, 2006 Share Posted September 8, 2006 To jefkin:You don't need to serialize any data that is put into a session variable, since all data is automagically serialized when the session array is stored.Ken Quote Link to comment Share on other sites More sharing options...
jefkin Posted September 8, 2006 Share Posted September 8, 2006 [quote author=kenrbnsn link=topic=107412.msg430979#msg430979 date=1157758672]To jefkin:You don't need to serialize any data that is put into a session variable, since all data is automagically serialized when the session array is stored.Ken[/quote]Izat so! ;Dlearn something new every day!Thanks Ken. Quote Link to comment Share on other sites More sharing options...
barkster Posted September 9, 2006 Author Share Posted September 9, 2006 Cool, wondered how you actually saw that, here is what I got so I assume I have to access it as multidimensional array$_SESSION['Names'][0][0]?? Why is it an array of arrays?Array( [0] => Array ( [0] => 10 [1] => John ))Thanks for all the suggestions, I'll try a the explode option also. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 9, 2006 Share Posted September 9, 2006 [quote author=barkster link=topic=107412.msg431032#msg431032 date=1157766675]Why is it an array of arrays?[/quote]Beacause your code "=array(split()) " makes it so.split() (or explode which is slightly more efficient in this case) returns the array you need. You are then placing this array into the first element of an array by placing array(..) round it. Quote Link to comment 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.