3

One of my functions is returning json encoded content using:

return json_encode($json, true);

Now in my code, calling this function I've already tried to use:

die(var_dump($result[0]));
die(var_dump($result["user"]));
die(var_dump($result->user));

None of this worked. When I dump the whole content I get returned, this is the output:

{"usd":1,"user":10000}
4
  • Why not you try die(var_dump($result)); alone and see what happens? Commented Jan 2, 2017 at 22:09
  • Have already tried this too, it's returning: string and the length. Commented Jan 2, 2017 at 22:09
  • 1
    You do know that json_encode()'s return value is a string, right? Commented Jan 2, 2017 at 22:10
  • Maybe you're looking for json_decode? Commented Jan 2, 2017 at 22:11

1 Answer 1

1

I am assuming that the $result variable is the content that is returned from the function.

You cannot access the properties inside that json string, before you decode it again. Therefore you will not be able to use $result["user"] or $result->user as the result contains a json string.

You need to decode it first:

$result = json_decode($result, true);

http://php.net/manual/en/function.json-decode.php

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

2 Comments

Works, thanks. What to do, so I can use $result->user instead of $result["user"]?
If you want the result to be an object, you have to decode it typing: $result = json_decode($result); (so without the ,true) then you can say $result->user. However if you want to have the result as an associative array so you can use $result['user'] you have to decode it as I wrote in the answer above.

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.