Php 如何从json文件中检索值?

Php 如何从json文件中检索值?,php,json,Php,Json,我试图从一个格式良好的json文件中检索值,该文件的值位于多维数组中。我的情况是天气预报 这是我正在处理的文件的示例。(很抱歉,文件太长,无法粘贴到此处) 这是我使用的代码,但它完全没有返回任何内容 <?php // Get Forecasts $json_string = file_get_contents("http://weather.mailchips.com/weather.json"); $parsed_json = json_decode($json

我试图从一个格式良好的json文件中检索值,该文件的值位于多维数组中。我的情况是天气预报

这是我正在处理的文件的示例。(很抱歉,文件太长,无法粘贴到此处)

这是我使用的代码,但它完全没有返回任何内容

<?php   
// Get Forecasts
$json_string = file_get_contents("http://weather.mailchips.com/weather.json");
$parsed_json = json_decode($json_string);
$forecasts = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'};

foreach ($forecasts as $forecast) { ?>
<div class="eachforecast">
<div class="fcday"><?php echo $forecast->{'title'};?></div>
<div class="fcicon"><?php echo $forecast->{'icon_url'};?></div>
<div class="fctext"><?php echo $forecast->{'fcttext_metric'};?></div>
</div>
<?php
}
?>

请帮帮我。我在php方面的知识不足以完成这项工作。

谢谢

代码在我这边运行得很好


但是我不知道为什么要用
$parsed_json->{'forecast'}
而不是
$parsed_json->forecast
来访问属性值。你的方法是可行的,但它既丑陋又不一致。

代码在我这方面效果很好


但是我不知道为什么要用
$parsed_json->{'forecast'}
而不是
$parsed_json->forecast
来访问属性值。您的方法是可行的,但它既丑陋又不一致。

您给出的代码绝对可以正常工作。 您应该检查是否允许
file\u get\u contents()
访问URL


您可以通过检查php.ini中的
allow\u url\u fopen
是否设置为1来实现这一点。

您给出的代码工作得非常好。 您应该检查是否允许
file\u get\u contents()
访问URL


您可以通过检查php.ini中的
allow\u url\u fopen
是否设置为1来执行此操作。

这与我的预期一样有效:

<?php
$content=file_get_contents('http://weather.mailchips.com/weather.json');
$content= json_decode($content);
foreach($content->forecast->txt_forecast->forecastday as $day){
?>
<div class="eachforecast" style=" width:120px;border:1px solid gray;display:inline-block;vertical-align:top;height:150px;overflow:auto">
    <div class="fcday"><?php echo $day->title;?></div>
    <div class="fcicon"><img src="<?php echo $day->icon_url;?>"></div>
    <div class="fctext"><?php echo $day->fcttext_metric;?></div>
</div>
<?php   
}
?>

图标\u url;?>">

在我这边,这正如期发挥作用:

<?php
$content=file_get_contents('http://weather.mailchips.com/weather.json');
$content= json_decode($content);
foreach($content->forecast->txt_forecast->forecastday as $day){
?>
<div class="eachforecast" style=" width:120px;border:1px solid gray;display:inline-block;vertical-align:top;height:150px;overflow:auto">
    <div class="fcday"><?php echo $day->title;?></div>
    <div class="fcicon"><img src="<?php echo $day->icon_url;?>"></div>
    <div class="fctext"><?php echo $day->fcttext_metric;?></div>
</div>
<?php   
}
?>

图标_url;?>“>

代码对我来说很好。检查您是否已启用php中的allow_url_fopen设置。尝试添加
echo$json\u字符串
查看是否正确地从服务器获取了json为什么要使用
$parsed_json->{'forecast'}
?这应该可以工作:
$parsed_json->forecast
如果该字段可用。@JerzyZawadzki是的,代码突然开始工作。谢谢Nicky De Maeyer,谢谢你的提示。提示:如果你将
json_decode
true
作为第二个参数传递,它将返回一个多维关联数组而不是对象。我发现这更容易使用。代码对我来说很好。检查您是否已启用php中的allow_url_fopen设置。尝试添加
echo$json\u字符串
查看是否正确地从服务器获取了json为什么要使用
$parsed_json->{'forecast'}
?这应该可以工作:
$parsed_json->forecast
如果该字段可用。@JerzyZawadzki是的,代码突然开始工作。谢谢Nicky De Maeyer,谢谢你的提示。提示:如果你将
json_decode
true
作为第二个参数传递,它将返回一个多维关联数组而不是对象。我发现这更容易处理。