Forum Moderators: coopster

Message Too Old, No Replies

Encoding / Decoding Multi-dimensional session array

         

designaweb

4:05 am on Jan 7, 2006 (gmt 0)

10+ Year Member



Maybe it has been asked before, but since I cannot search the site, I will ask again.

Can you encode and decode multi-dimensional arrays? I think encoding them works just fine using session_encode(), however, session_decode is only capable of decoding the single dimensional arrays stored in the encoded string...

Any ideas?

jatar_k

4:52 am on Jan 7, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what about serialize [php.net]

serialize() handles all types, except the resource-type. You can even serialize() arrays that contain references to itself. References inside the array/object you are serialize()ing will also be stored.

designaweb

5:57 am on Jan 7, 2006 (gmt 0)

10+ Year Member



Hmmmmm....

It seems to be working. I will report back if I'm 100% sure!

... designaweb wonders why session_encode() / session_decode() was ever invented if serialize() / unserialize () also work ...

coopster

8:01 pm on Jan 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




however, session_decode is only capable of decoding the single dimensional arrays stored in the encoded string

session_decode() [php.net] will completely repopulate your session with the string you pass to it. For example, let's say you used session_encode() [php.net] to store a session in a string variable and you write that string out to a file. Later, you can retrieve the contents of that file and after you start_session() you can use the decode function on that string and it will populate your $_SESSION again for you, including multi-dimensional arrays.

session_encode/session_decode is different from serialize/unserialize in that is specific to the $_SESSION superglobal, but that is really about the only difference.