Jump to content

Archived

This topic is now archived and is closed to further replies.

barkster

Session Variable Array

Recommended Posts

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"

Share this post


Link to post
Share on other sites
Do a
[code]<?php
echo '<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

Share this post


Link to post
Share on other sites
Maybe a better solution is to store your array data with serailize()/unserialize()

saver:

$_SESSION['names'] = serialize($names);

....

reader:

$names = unserialize($_SESSION['names']);

....


HTH

Jeff

Share this post


Link to post
Share on other sites
Don't forget to put

session_start();

at top of script if you are going to be using $_SESSION

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[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!  ;D

learn something new every day!

Thanks Ken.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites

×

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.