亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 json 中的子行中刪除方括號

從 json 中的子行中刪除方括號

PHP
九州編程 2023-07-08 21:43:07
我正在嘗試在網站上顯示以下數據:-"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">&nbsp;Minimum Temperature&nbsp;<?php echo $data->daily[0]->clouds; ?>&deg;C</span><br>   <span class="min-temperature">&nbsp;Pressure&nbsp;<?php echo $data->daily[0]->weather->id; ?></span>第一行顯示數據正常,但天氣部分中的任何內容都無法顯示任何內容。我已經看到了刪除所有方括號的解決方案,但它只需要圍繞天氣部分的括號。提前致謝
查看完整描述

2 回答

?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

下面的代碼 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; 

http://img1.sycdn.imooc.com//64a9680f0001002006530169.jpg

查看完整回答
反對 回復 2023-07-08
?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

在這種情況下,您必須使用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如果您想知道數組索引是如何工作的,您可以從代碼中使用它,只需將其從注釋中刪除即可。


查看完整回答
反對 回復 2023-07-08
  • 2 回答
  • 0 關注
  • 196 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號