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
11.1k11 gold badges67 silver badges112 bronze badges
asked Dec 17, 2011 at 14:07
1

1 Answer 1

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.

answered Dec 17, 2011 at 14:10
Sign up to request clarification or add additional context in comments.

1 Comment

When adding brackets and stripping the backslashes it works, thanks.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.