0

I have IOS dictionary like

[{\"my_id_one\":16403,\"my_id_two\":7239}]

When I decode using json_decode, it returns null.

What is best way to decode it to convert it to a PHP array?

3
  • Works fine for me... 3v4l.org/8SPnd Commented Mar 18, 2016 at 16:44
  • you have to un-escape slashes, if your example is correct. what is the origin of above string? Commented Mar 18, 2016 at 16:46
  • If you want to escape the " make sure you use " to enclose the string. Commented Mar 18, 2016 at 16:47

1 Answer 1

1

Take a look at stripcslashes():

$str = '[{\"my_id_one\":16403,\"my_id_two\":7239}]';
$str_unescaped = stripcslashes($str);
$array = json_decode($str_unescaped, true);

print_r($array);

This outputs:

Array ( [0] => Array ( [my_id_one] => 16403 [my_id_two] => 7239 ) )

Sign up to request clarification or add additional context in comments.

1 Comment

yes it works when you create string in PHP as you did. but when a dictionary is submitted by IOS, it won't decode properly. I have already tried this but didn't work. When i test it with RESTclient, it works but in case IOS, i won't. I also tried all methods on php.net/manual/en/function.json-decode.php Nothing get work and then i finally post my question to stackover. anyway thanks for the help

Your Answer

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.