用 PHP 显示 JSON 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10967770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying JSON data with PHP
提问by David Morin
I have the following array
我有以下数组
"forecast":{
"txt_forecast": {
"date":"11:00 PM EDT",
"forecastday": [
{
"period":0,
"icon":"partlycloudy",
"icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title":"Saturday",
"fcttext":"Partly cloudy in the morning, then mostly cloudy. High of 88F. Winds from the South at 5 to 10 mph.",
"fcttext_metric":"Partly cloudy in the morning, then mostly cloudy. High of 31C. Winds from the South at 10 to 15 km/h.",
"pop":"0"
}
,
{
"period":1,
"icon":"partlycloudy",
"icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title":"Saturday Night",
"fcttext":"Overcast in the evening, then partly cloudy. Low of 64F. Winds less than 5 mph.",
"fcttext_metric":"Overcast in the evening, then partly cloudy. Low of 18C. Winds less than 5 km/h.",
"pop":"10"
}
How would I display the information in PHP? It goes all the way up to period 7.
我将如何在 PHP 中显示信息?它一直持续到第 7 期。
EDIT: Here is my edited code that is not working.
编辑:这是我编辑的代码不起作用。
<?php
$json_string = file_get_contents("http://api.wunderground.com/api/7ec5f6510a4656df/geolookup/forecast/q/40121.json");
$parsed_json = json_decode($json_string);
$temp = $parsed_json->{'forecast'}->{'txt_forecast'}->{'date'};
$title = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}->{'title'};
$for = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}->{'fcttext'};
echo "Current date is ${temp}, ${title}: ${for}\n";
foreach($parsed_json['forecast']['forecastday[0]'] as $key => $value)
{
echo $value['period'];
echo $value['icon'];
// etc
}
?>
回答by Sarfraz
Update
更新
$json_string = file_get_contents("http://api.wunderground.com/api/7ec5f6510a4656df/geolookup/forecast/q/40121.json");
$parsed_json = json_decode($json_string, true);
$parsed_json = $parsed_json['forecast']['txt_forecast']['forecastday'];
//pr($parsed_json);
foreach($parsed_json as $key => $value)
{
echo $value['period'] . '<br>';
echo $value['icon'] . '<br>';
// etc
}
Result:
结果:
0
chancetstorms
1
tstorms
2
tstorms
3
tstorms
4
tstorms
5
partlycloudy
6
partlycloudy
7
partlycloudy
You will have to use json_decode()function for that.
您将不得不为此使用json_decode()功能。
$myArray = json_decode($yourJSON, true);
Now you can use foreachto get the data from $myArrayarray eg:
现在您可以使用foreach从$myArray数组中获取数据,例如:
foreach($myArray['forecast']['forecastday'] as $key => $value)
{
echo $value['period'];
echo $value['icon'];
// etc
}
回答by Eric
You can also convert json to PHP objects:
您还可以将 json 转换为 PHP 对象:
$myData = json_decode($yourJSON);
foreach($myData->forecast->forecastday as $day) {
echo $day->period;
echo $day->icon;
}
EDIT:
编辑:
I think you can fix the code in your question with this:
我认为您可以使用以下方法修复问题中的代码:
<?php
$json_string = file_get_contents("http://api.wunderground.com/api/7ec5f6510a4656df/geolookup/forecast/q/40121.json");
$parsed_json = json_decode($json_string);
$forecast = $parsed_json->forecast->txt_forecast;
$temp = $forecast->date;
$title = $forecast->forecastday->title;
$for = $forecast->forecastday->fcttext;
echo "Current date is ${temp}, ${title}: ${for}\n";
foreach($forecast->forecastday as $value) {
echo $value['period'];
echo $value['icon'];
// etc
}
?>

