views:

92

answers:

1

Can I set a multi-dimensional array 30x200 in JavaScript as a cookie and then read it back in PHP?

+1  A: 

You can if you want. The normal maximum size for a cookie header is 4Kb, so you would do well to ensure that your data size does not exceed that.

PHP and Javascript obviously have different syntactical methods of dealing with data, so you'd have to serialize your array somehow and perform the reverse in PHP to get the data into a usable form. If you need to keep specific array syntax, you could encode the data into a JSON string and use json_decode() in PHP. Another method would be to find a serialization class (see this article) for Javascript. Perhaps even a comma-delimited string would work that you could just explode() in PHP.

It sounds like there could be a better way to do this than cookies though. You may want to re-examine your methods. Perhaps you could store the dates in $_SESSION somehow?

zombat
I have to interject and recommend the phpjs.org serialize function here.
munch
@fucla - thanks, that looks good. I've never actually needed to bother serializing JS data, so I'll have to remember that.
zombat