Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 688
Andres SK Avatar asked Dec 11 '12 18:12

Andres SK


People also ask

How to convert JSON data to PHP array?

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.

How to convert JSON object to string PHP?

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);

What is a function in PHP that transforms JSON into an object?

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.


2 Answers

Try

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

like image 190
GBD Avatar answered Oct 17 '22 22:10

GBD


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

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

otherwise you may get stdClass error

like image 13
Roy B. xSITE Avatar answered Oct 17 '22 21:10

Roy B. xSITE