Lewis Smallwood
Lewis Smallwood

Reputation: 84

JSON decode string in PHP (array of arrays) Special characters

So I have this issue when converting a JSON string to a PHP array. The data is sent via HTTP POST so I'm aware there may be some decoding needed.

Could anyone lay an insight on how I would use json_decode() in PHP to convert this string to an array? "[[\"37\",\"text\",\"\\\"\\\"\"],[\"38\",\"text\",\"\\\"\\\"\"],[\"39\",\"text\",\"\\\"one word two words. Hello? \\\\\\\"escape\\\\\\\" lol\\\"\"]]"

The input was:

[
    ["37", "text", ""],
    ["38", "text", ""],
    ["39", "text", userInputtedString]
]

Where userInputtedString is: one word two words. Hello? "escape" lol

^ Or any other Unicode values

Upvotes: 0

Views: 1199

Answers (3)

DsRaj
DsRaj

Reputation: 2328

Use utf8_encode before json_decode

$str = "[[\"37\",\"text\",\"\\\"\\\"\"],[\"38\",\"text\",\"\\\"\\\"\"],[\"39\",\"text\",\"\\\"one word two words. Hello? \\\\\\\"escape\\\\\\\" lol\\\"\"]]";
$str = utf8_encode($str);
$str = json_decode($str,JSON_UNESCAPED_SLASHES);

Upvotes: 2

Hiren Makwana
Hiren Makwana

Reputation: 2156

You could also use uft8_encode (to send to HTML) and uft8_decode (to receive) but not the right way

Upvotes: 1

Swakeert Jain
Swakeert Jain

Reputation: 776

What seems to be the problem?

Simply use json_decode like you mentioned.

$ans = json_decode($_POST["name-of-var"]);

This should do it.

Upvotes: 1

Related Questions