【发布时间】:2015-01-15 20:08:04
【问题描述】:
我想从 json 解码结果中回显一个数组数据,所以我尝试了 file_get_contents 和 curl 都可以。
这是我从服务器得到的响应,它是 json 输出..
{"Servers": {
"lastChangedDate": null,
"ServersList": [
{
"online": "The server is UP",
"offline": "The server is DOWN",
"maintainace": "The server is currently in maintenance mode",
"location": "EU",
"ip": "x.x.x.x"
},
{
"online": "The server is UP",
"offline": "The server is DOWN",
"maintainace": "The server is currently in maintenance mode",
"location": "US",
"ip": "x.x.x.x"
}
]
}}
现在解码后输出将是这样的数组..
Array (
[Servers] => Array (
[lastChangedDate] =>
[ServersList] => Array (
[0] => Array (
[online] => The server is UP
[offline] => The server is down
[maintenance] => The server is currently in maintenance mode
[location] => EU
[ip] => x.x.x.x
)
[1] => Array (
[online] => The server is UP
[offline] => The server is DOWN
[maintenance] => The server is currently in maintenance mode
[location] => US
[ip] => x.x.x.x
)
)
)
)
这是我的 php 代码
<?php
$request = file_get_contents("test.json");
$input = json_decode($request, true);
echo $input['Servers']['lastChangedDate']['ServersList'][0]['online'];
?>
带有 print_r ($input) 的演示;而不是回显http://phpad.org/run/1666334020
所以在我的主页中我想输出像这样http://codepen.io/anon/pen/jEEPMG.html
【问题讨论】:
-
$input['Servers']['ServersList'][0]['online'];应该可以工作 -
那行得通..谢谢..您可以发布为答案,以便我可以将其标记为答案。
-
print_r 输出如果显示正确会缩进。只需按照下一行的缩进即可。