我有一个包含服务器列表的JSON:
{
"serverlist": {
"live": {
"Echo server": {
"age": "00:05:39",
"title": "sometitle"
},
"Rio server": {
"age": "00:02:57",
"title": "sometitle"
},
"Goodies server": {
"age": "00:04:14",
"title": "sometitle"
},
"Ben server": {
"age": "00:03:30",
"title": "sometitle"
}
}
}
}
我有这段代码:
$json_string = file_get_contents("jsonurl");
$parsed_json = json_decode($json_string, true);
$parsed_json = $parsed_json['serverlist']['live'];
foreach($parsed_json as $key => $value)
{
echo $value['age'] . '<br>';
echo $value['tist'] . '<br>';
}
我试图获取每个服务器名称(echo服务器,rio服务器等)。 我怎么能这样做?
答案 0 :(得分:2)
服务器名称是密钥,因此它位于$key
变量中。
foreach($parsed_json as $key => $value)
{
echo $key . '<br>';
echo $value['age'] . '<br>';
echo $value['tist'] . '<br>';
}