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!
To convert a JSON data string to a PHP array, you can use the json_decode() function. The json_decode() function accepts the JSON string as the first parameter and a few additional parameters to control the process of converting JSON to PHP array.
People use json_encode() and json_decode() to convert objects/arrays of PHP to string and back to objects/arrays. Save this answer. Show activity on this post. $array = json_decode($json,true); $string = implode(",",$array);
This function ( json_decode() ) basically converts a JSON string into an object. It takes JSON string as a parameter and converts it into an object.
Try
json_decode(stripslashes($_COOKIE['data']));
a small fix to the answer above (can't comment yet)...
json_decode(stripslashes($_COOKIE['data']),true);
otherwise you may get stdClass error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With