9

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!

6
  • 1
    if you use JSON.stringify before passing it to php, json_decode() should be all you need. can we see more of your code? Commented Dec 11, 2012 at 18:28
  • 2
    it appears you need to stripslashes(). Commented Dec 11, 2012 at 18:28
  • I think json_decode is having hard time understanding your current structure because of the slashes. Commented Dec 11, 2012 at 18:30
  • 1
    possible duplicate of How to turn off magic quotes on shared hosting? Commented Dec 11, 2012 at 18:40
  • 1
    I don't think that really is a duplicate of that other quesiton. Commented Dec 11, 2012 at 23:16

2 Answers 2

26

Try

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

Sign up to request clarification or add additional context in comments.

3 Comments

The the square bracket enclosure also needs to be stripped.
@Lenin i didn't get your point. anyway here is working sample: codepad.org/XX9QD3iX
Yeah, this was the issue. Thanks!
13

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

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

otherwise you may get stdClass error

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.