如何显示特定的json值而不是print或dump函数

Mr *_*ose 2 php json

使用下面的代码,我可以将所有信息显示为两种格式的数组,但是我希望为变量赋值,并使用例如名称而不是完整的屏幕转储.

$ url ='http:// myurl';

$ json = file_get_contents($ url);

$ dump =(var_dump(json_decode($ json,true)));

$ json_output = json_decode($ json); 的print_r($ json_output)

我很抱歉,这可能很容易.

Dal*_*ale 6

您可以使用:

$object = json_decode($json);
Run Code Online (Sandbox Code Playgroud)

这将创建一个对象,然后您可以访问类似的属性..

echo $object->whatever;
Run Code Online (Sandbox Code Playgroud)

或者您可以像这样使用json_decode:

$array = json_decode($json, TRUE);
Run Code Online (Sandbox Code Playgroud)

这创建了一个数组,你可以访问这样的单个键.

echo $array['whatever'];
Run Code Online (Sandbox Code Playgroud)