Andres SK
Andres SK

Reputation: 10974

Converting a stringified json structure to PHP array

I'm saving a cookie with json data. Example of echo $_COOKIE['data']

[{\"date\":1355249777,\"title\":\"junior\"},{\"date\":1355249747,\"title\":\"christopher\"},{\"date\":1355249139,\"title\":\"melfi\"},{\"date\":1355249123,\"title\":\"tony\"},{\"date\":1355248876,\"title\":\"carmela\"},{\"date\":1355248859,\"title\":\"meadow\"}]

The data was pure javascript, then passed by JSON.stringify and then stored in the cookie. Now i need to convert it to a php array. I tried a json_decode approach but it returns null. Any ideas? Thanks!

Upvotes: 9

Views: 21903

Answers (2)

Roy B. xSITE
Roy B. xSITE

Reputation: 141

a small fix to the answer above (can't comment yet)...

json_decode(stripslashes($_COOKIE['data']),true);

otherwise you may get stdClass error

Upvotes: 13

GBD
GBD

Reputation: 15981

Try

json_decode(stripslashes($_COOKIE['data']));

Upvotes: 26

Related Questions