1

I'm sending a stringified json object to a php page. I want to loop through it in php. When I use:

echo $_POST['Tags'];

that results in:

{\"0\":\"tag1\",\"1\":\"tag2\"}

but

echo json_decode($_POST['Tags'], true/false);

doesn't print anything. Shouldn't I at least get Array?

Josh
  • 10,961
  • 11
  • 65
  • 108
Johan
  • 35,120
  • 54
  • 178
  • 293
  • possible duplicate of [How can I disable PHP magic quotes at runtime?](http://stackoverflow.com/questions/1153741/how-can-i-disable-php-magic-quotes-at-runtime) – mario Dec 17 '11 at 14:15

1 Answers1

2
php > var_dump(json_decode('{\"0\":\"tag1\",\"1\":\"tag2\"}'));
NULL

You really should use var_dump if you don't get any visible output. And as the php.net documentation ( states:

Return Values

Returns the value encoded in json in appropriate PHP type. Values true, false and null (case-insensitive) are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

your JSON is invalid.
The correct JSON would be:

["tag1","tag2"]

or

{"0":"tag1","1":"tag2"}

Without the Backslashes.

Community
  • 1
  • 1
TimWolla
  • 31,849
  • 8
  • 63
  • 96