Check value i foreach loop if else

Hello i would like to check if a value for a specific row and specific column is above value and then execute stuff.

This is what i have so far.

<?php
// start på API för att hämta data från telldus
// Get cURL resource
$ch = curl_init();

// Set url
curl_setopt($ch, CURLOPT_URL, 'https://api.telldus.com/json/sensors/list?includeIgnored&includeValues=1&includeScale&useAlternativeData=1');

// Set method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

// Set options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//Insert your API keys here 
$oauth_consumerKey = "*********";
$oauth_privateKey = "**********";
$oauth_token = "**********";
$oauth_tokenSecret = "***********";

$oauth_signature = ($oauth_privateKey . "%26" . $oauth_tokenSecret);
$oauth_nonce = md5(uniqid(mt_rand(), true));
$oauth_timestamp = $_SERVER['REQUEST_TIME'];

// Set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, [
  "Authorization: OAuth oauth_consumer_key=" . $oauth_consumerKey . ", oauth_nonce=" . $oauth_nonce . ", oauth_signature=" . $oauth_signature .", oauth_signature_method=\"PLAINTEXT\", oauth_timestamp=" . $oauth_timestamp . ", oauth_token=" . $oauth_token . ", oauth_version=\"1.0\"",
 ]
);

// 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 $key => $sensor) {
// SLUT på API för att hämta data från Telldus

echo $key ;
echo $sensor['id']."<br />";
 ?>

<?php }  ?>
<div class="row">
<div class="container-fluid">
    <?php if (($sensor['temp']) > 40){ ?>
<div class="col"><b>JUST NU ELDAS DET I BASTUN </b><i class="fab fa-hotjar text-danger" aria-hidden="true"></i><br />
    <?php } else { ?>
<div class="col"><b>JUST NU I BASTUN</b><br />
    <?php

   } ?>

echo $key generates 0 1 2 3
echo $sensor[‘id’] generates the four sensor id’s

The array looks like this:

Array ( 
[id] => 1529882289 [name] => Badet [lastUpdated] => 1570190943 [ignored] => 0 [client] => 451789 [clientName] => Bastun [online] => 1 [editable] => 1 [battery] => 254 [keepHistory] => 1 [protocol] => fineoffset [model] => temperature [sensorId] => 6 [miscValues] => {"chId": 184} [temp] => 4.6 
) 

Array ( 
[id] => 1529964226 [name] => Bastun [lastUpdated] => 1570190876 [ignored] => 0 [client] => 451789 [clientName] => Bastun [online] => 1 [editable] => 1 [battery] => 254 [keepHistory] => 1 [protocol] => fineoffset [model] => temperature [sensorId] => 8 [miscValues] => {"chId": 114} [temp] => 5.3 
) 

Array ( 
[id] => 1529963981 [name] => Loungen [lastUpdated] => 1570190798 [ignored] => 0 [client] => 451789 [clientName] => Bastun [online] => 1 [editable] => 1 [battery] => 254 [keepHistory] => 1 [protocol] => fineoffset [model] => temperaturehumidity [sensorId] => 7 [miscValues] => {"chId": 247} [temp] => 5.2 [humidity] => 67 
) 

Array ( 
[id] => 1529882254 [name] => Ute [lastUpdated] => 1570190763 [ignored] => 0 [client] => 451789 [clientName] => Bastun [online] => 1 [editable] => 1 [battery] => 254 [keepHistory] => 1 [protocol] => fineoffset [model] => temperaturehumidity [sensorId] => 5 [miscValues] => {"chId": 183} [temp] => 5.1 [humidity] => 70 
)

I want to check something like this if ($sensor['id] == 1529964226 ) and ($sensor[‘temp’] > 40) { echo stuff } else { echo stuff }

If you need to reference individual row(s) of data, I recommend that you index/pivot the data using the id value as the index. You can then directly access the data by its id. Your existing foreach(){} code would become -

$data = []; // define an array to hold the indexed data
foreach($eachSensor as $row)
{
    $data[$row['id']] = $row;
}

For your example, you can then just directly access the correct row of data -

$id = 1529964226; // which sensor data to operate on
if($data[$id]['temp'] > 40])
{
    // temperature is over 40

} else {
    // temperature is 40 or less

}

If the data may not exist, you would add an isset() test -

if(!isset($data[$id])) // note the not ! operator
{
    // there is no data for the $id

} else {
    // there is data for the id, the logic shown above would go here...

}

Amazeballs! I will try this. Thanks.
And it works like a charm!

Sponsor our Newsletter | Privacy Policy | Terms of Service