[英]PHP - Grab info from array
我試圖從我的氣象站獲取傳感器值。 我得到的 API 在數組中。
我不知道如何只從 ["id"]=> "1549112871" 中獲取 "temp" 數據。 如果我回顯 $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" }
作為您提供的數據,您的數據是一個二維數組。 每個數組都有鍵:“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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.