我試圖從我的氣象站獲取傳感器值。我得到的 API 在陣列中。
我不知道如何只從 [“id”]=>“1549112871”中獲取“臨時”資料。如果我 echo $sensor["temp"] 我將從整個陣列中獲取臨時資料。
// Send the request & save response to $resp
$resp = curl_exec($ch);
$jsonResp = json_decode($resp, true);
$eachSensor = $jsonResp["sensor"];
// Close request
curl_close($ch);
foreach ($eachSensor as $sensor) {
?>
<h2><?php if(!empty($sensor["rrate"])) { echo $sensor["rrate"] . " mm nederb?rd senaste
dygnet"; } else { /* */ }?></h2>
<?php } ?>
<h3><?php echo "Det bl?ser f?r tillf?llet " . $sensor["wgust"] . " m/s";?></h3>
array(16) { ["id"]=> string(10) "1547443848" ["name"]=> string(9) "Friggebod"
["lastUpdated"]=> int(1640767771) ["ignored"]=> int(0) ["client"]=> string(6) "592885"
["clientName"]=> string(3) "Hem" ["online"]=> string(1) "1" ["editable"]=> int(1)
["battery"]=> int(254) ["keepHistory"]=> int(1) ["protocol"]=> string(10) "fineoffset"
["model"]=> string(19) "temperaturehumidity" ["sensorId"]=> string(2) "15" ["miscValues"]=>
string(13) "{"chId": 151}" ["temp"]=> string(3) "3.4" ["humidity"]=> string(2) "62" }
array(16) { ["id"]=> string(10) "1549112896" ["name"]=> string(10) "Regnsensor"
["lastUpdated"]=> int(1640767565) ["ignored"]=> int(0) ["client"]=> string(6) "592885"
["clientName"]=> string(3) "Hem" ["online"]=> string(1) "1" ["editable"]=> int(1)
["battery"]=> int(253) ["keepHistory"]=> int(1) ["protocol"]=> string(6) "oregon" ["model"]=>
string(4) "2914" ["sensorId"]=> string(2) "31" ["miscValues"]=> string(12) "{"chId": 19}"
["rrate"]=> string(1) "0" ["rtot"]=> string(4) "49.5" }
array(16) { ["id"]=> string(10) "1549112871" ["name"]=> string(7) "Ute Tak" ["lastUpdated"]=>
int(1640767645) ["ignored"]=> int(0) ["client"]=> string(6) "592885" ["clientName"]=>
string(3) "Hem" ["online"]=> string(1) "1" ["editable"]=> int(1) ["battery"]=> int(254)
["keepHistory"]=> int(1) ["protocol"]=> string(6) "oregon" ["model"]=> string(4) "F824"
["sensorId"]=> string(2) "29" ["miscValues"]=> string(12) "{"chId": 19}" ["temp"]=> string(4)
"-0.1" ["humidity"]=> string(2) "97" }
uj5u.com熱心網友回復:
作為您提供的資料,您的資料是一個二維陣列。每個陣列都有鍵:“id”、“temp”、...并且您想獲得具有特定“id”的陣列的“temp”值。
如果您想這樣做,您必須遍歷陣列以查找與所需“id”匹配的陣列:
foreach ($eachSensor as $sensor) {
if ($sensor['id'] != '1549112871') {
continue;
}
// Your code here...
// This $sensor has the id value equal to 1549112871
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/396872.html
標籤:php
