简体   繁体   English

php从函数返回json编码的字符串

[英]php return json encoded string from function

One of my functions is returning json encoded content using: 我的一个功能是使用以下命令返回json编码的内容:

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}

I am assuming that the $result variable is the content that is returned from the function. 我假设$ result变量是从函数返回的内容。

You cannot access the properties inside that json string, before you decode it again. 在再次解码之前,您无法访问该json字符串中的属性。 Therefore you will not be able to use $result["user"] or $result->user as the result contains a json string. 因此,您将无法使用$ result [“user”]或$ result-> user,因为结果包含json字符串。

You need to decode it first: 您需要先解码它:

$result = json_decode($result, true);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM