从 json 中的子行中删除方括号
Remove square brackets from child rows in json
我正在尝试在网站上显示以下数据:-
"daily":[{"dt":1593864000,"sunrise":1593834201,"sunset":1593894929,"temp":{"day":18.47,"min":17.83,"max":18.71,"night":17.83,"eve":18.71,"morn":18.47},"feels_like":{"day":16,"night":13.09,"eve":16.54,"morn":16},"pressure":1006,"humidity":77,"dew_point":14.37,"wind_speed":5.51,"wind_deg":244,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":90,"uvi":7.08}
我提取了除天气部分中的条目之外的任何条目,因为我使用的代码认为天气数据是一个单独的数组。
与显示数据相关的代码部分是:-
<span class="min-temperature"> Minimum Temperature <?php echo $data->daily[0]->clouds; ?>°C</span><br>
<span class="min-temperature"> Pressure <?php echo $data->daily[0]->weather->id; ?></span>
第一行显示数据正常,但天气部分中的任何内容都无法显示任何内容。
我已经看到了删除所有方括号的解决方案,但它只需要天气部分周围的括号。
提前致谢
json_decodes 下面的代码与云和天气数组相呼应。 '希望能帮助到你。请评论。谢谢。
<?php
$data=json_decode( '{"daily":{"dt":1593864000,"sunrise":1593834201,"sunset":1593894929,"temp":{"day":18.47,"min":17.83,"max":18.71,"night":17.83,"eve":18.71,"morn":18.47},"feels_like":{"day":16,"night":13.09,"eve":16.54,"morn":16},"pressure":1006,"humidity":77,"dew_point":14.37,"wind_speed":5.51,"wind_deg":244,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":90,"uvi":7.08}}'); # define $data as a stdClass Object
echo $data->daily->clouds;
echo "\n";
# below, weather array is converted into a string
$wa=(array)$data->daily->weather[0];
foreach($wa as $key=> $val){
echo $key."=".$val."; ";
}
?>
输出:
90
id=500; main=Rain; description=light rain; icon=10d;
在这种情况下,您必须使用 json_decode
将 json 字符串转换为关联数组。
$data = '{"daily":{"dt":1593864000,"sunrise":1593834201,"sunset":1593894929,"temp":{"day":18.47,"min":17.83,"max":18.71,"night":17.83,"eve":18.71,"morn":18.47},"feels_like":{"day":16,"night":13.09,"eve":16.54,"morn":16},"pressure":1006,"humidity":77,"dew_point":14.37,"wind_speed":5.51,"wind_deg":244,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":90,"uvi":7.08}}';
$decode = json_decode($data,true);
echo '<pre>';
//print_r($decode);
echo $decode['daily']['clouds'].'<br>';
echo $decode['daily']['uvi'].'<br>';
echo $decode['daily']['weather'][0]['id'].'<br>';
echo $decode['daily']['weather'][0]['main'].'<br>'; //These three are from weather array.
echo $decode['daily']['weather'][0]['description'].'<br>';
echo '<pre>';
输出
90
7.08
500
Rain
light rain
如果您想知道数组索引是如何工作的,您可以使用代码中的 print_r
,只需将其从注释中删除即可。
我正在尝试在网站上显示以下数据:-
"daily":[{"dt":1593864000,"sunrise":1593834201,"sunset":1593894929,"temp":{"day":18.47,"min":17.83,"max":18.71,"night":17.83,"eve":18.71,"morn":18.47},"feels_like":{"day":16,"night":13.09,"eve":16.54,"morn":16},"pressure":1006,"humidity":77,"dew_point":14.37,"wind_speed":5.51,"wind_deg":244,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":90,"uvi":7.08}
我提取了除天气部分中的条目之外的任何条目,因为我使用的代码认为天气数据是一个单独的数组。
与显示数据相关的代码部分是:-
<span class="min-temperature"> Minimum Temperature <?php echo $data->daily[0]->clouds; ?>°C</span><br>
<span class="min-temperature"> Pressure <?php echo $data->daily[0]->weather->id; ?></span>
第一行显示数据正常,但天气部分中的任何内容都无法显示任何内容。
我已经看到了删除所有方括号的解决方案,但它只需要天气部分周围的括号。
提前致谢
json_decodes 下面的代码与云和天气数组相呼应。 '希望能帮助到你。请评论。谢谢。
<?php
$data=json_decode( '{"daily":{"dt":1593864000,"sunrise":1593834201,"sunset":1593894929,"temp":{"day":18.47,"min":17.83,"max":18.71,"night":17.83,"eve":18.71,"morn":18.47},"feels_like":{"day":16,"night":13.09,"eve":16.54,"morn":16},"pressure":1006,"humidity":77,"dew_point":14.37,"wind_speed":5.51,"wind_deg":244,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":90,"uvi":7.08}}'); # define $data as a stdClass Object
echo $data->daily->clouds;
echo "\n";
# below, weather array is converted into a string
$wa=(array)$data->daily->weather[0];
foreach($wa as $key=> $val){
echo $key."=".$val."; ";
}
?>
输出:
90
id=500; main=Rain; description=light rain; icon=10d;
在这种情况下,您必须使用 json_decode
将 json 字符串转换为关联数组。
$data = '{"daily":{"dt":1593864000,"sunrise":1593834201,"sunset":1593894929,"temp":{"day":18.47,"min":17.83,"max":18.71,"night":17.83,"eve":18.71,"morn":18.47},"feels_like":{"day":16,"night":13.09,"eve":16.54,"morn":16},"pressure":1006,"humidity":77,"dew_point":14.37,"wind_speed":5.51,"wind_deg":244,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":90,"uvi":7.08}}';
$decode = json_decode($data,true);
echo '<pre>';
//print_r($decode);
echo $decode['daily']['clouds'].'<br>';
echo $decode['daily']['uvi'].'<br>';
echo $decode['daily']['weather'][0]['id'].'<br>';
echo $decode['daily']['weather'][0]['main'].'<br>'; //These three are from weather array.
echo $decode['daily']['weather'][0]['description'].'<br>';
echo '<pre>';
输出
90
7.08
500
Rain
light rain
如果您想知道数组索引是如何工作的,您可以使用代码中的 print_r
,只需将其从注释中删除即可。