0

I am trying to evaluate the content of an array. The array contain water temperatures submitted by a user.

The user submits 2 temperaures, one for hot water and one for cold water.

What I need is to evaluate both array items to find if they are within the limits, the limits are "Hot water: between 50 and 66", "Cold water less than 21".

If either Hot or Cold fail the check flag the Status "1" or if they both pass the check flag Status "0".

Below is the code I am working with:

$row_WaterTemp['HotMin'] = "50";
$row_WaterTemp['Hotmax'] = "66";

$SeqWaterArray new(array);

$SeqWaterArray = array("58", "21");


foreach($SeqWaterArray as $key => $val) {
    $fields[] = "$field = '$val'";
    if($key == 0) { 
        if($val < $row_WaterTemp['HotMin'] || $val > $row_WaterTemp['Hotmax']) {
            $Status = 1;
            $WaterHot = $val;
        } else {
            $Status = 0;
            $WaterHot = $val;
        }

    }

    if($key == 1) {
        if($val > $row_WaterTemp['ColdMax']) {
            $Status = 1;
            $WaterCold = $val;
        } else {
            $Status = 0;
            $WaterCold = $val;
        }
    }   

}

My question is:

When I run the script the array key(0) works but when the array key(1) is evaluted the status flag for key(1) overrides the status flag for key0. If anyone can help that would be great. Many thanks for your time.

DCJones
  • 3,121
  • 5
  • 31
  • 53
  • And your question is? – u_mulder Sep 07 '16 at 11:15
  • Sorry, forgot to ask the question. Now I have. – DCJones Sep 07 '16 at 11:19
  • Add a `$row_WaterTemp['ColdMax'] = ...` to your question? What does this do? `$SeqWaterArray new(array);`? should it be: `$SeqWaterArray = array();`? May I suggest you turn on 'display errors' - see: http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Ryan Vincent Sep 07 '16 at 12:04

4 Answers4

0

It seems OK to me, exept for the values at limit, and you can simplify

$row_WaterTemp['HotMin'] = "50";
$row_WaterTemp['Hotmax'] = "66";

$SeqWaterArray = array("58", "21");
$Status = array() ;

foreach($SeqWaterArray as $key => $val) {
    if($key == 0) { 
        $Status = ($val >= $row_WaterTemp['HotMin'] && $val <= $row_WaterTemp['Hotmax']) ;
        $WaterHot = $val;
    } else if($key == 1) {
        $Status += ($val >= $row_WaterTemp['ColdMax']) ;
        $WaterCold = $val;
    }
}

If one fails, $Status = 1, if the two tests failed, $Status = 2, if it's ok, $Status = 0.

Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
0
<?php

// this function return BOOL (true/false) when the condition is met
function isBetween($val, $min, $max) {
  return ($val >= $min && $val <= $max);
}

$coldMax = 20; $hotMin = 50; $hotMax = 66;

// I decided to simulate a test of more cases:
$SeqWaterArray['john'] = array(58, 30);
$SeqWaterArray['martin'] = array(34, 15);
$SeqWaterArray['barbara'] = array(52, 10);

foreach($SeqWaterArray as $key => $range) {
    $flag = array();
    foreach($range as $type => $temperature) {
        // here we fill number 1 if the temperature is in range 
        if ($type == 0) {
            $flag['hot'] = (isBetween($temperature, $hotMin, $hotMax) ? 0 : 1);     
        } else {
            $flag['cold'] = (isBetween($temperature, 0, $coldMax) ? 0 : 1);     
        }                
    }
    $results[$key]['flag'] = $flag;                
}

var_dump($results);

?>

This is the result:

["john"]=> 
     "flag"=> 
        ["hot"]=> 1
        ["cold"]=> 0 
["martin"]=> 
     "flag" => 
        ["hot"]=> 1 
        ["cold"]=> 0 
["barbara"]=> 
    "flag" => 
        ["hot"]=> 0 
        ["cold"]=> 0 
pedrouan
  • 12,762
  • 3
  • 58
  • 74
  • ..and what happened to the check for cold water? – simon Sep 07 '16 at 11:28
  • Yes you test both temperatures but only whether they are within the limits for hot water. If you look at OPs' code you will notice that there is a check for a cold water limit as well. – simon Sep 07 '16 at 11:35
  • You better take a look at OPs' question or his code. Your function checks whether the temperature is between 55 and 66 for **both** values. But OP wants the second temperature to be checked against the cold water limit. – simon Sep 07 '16 at 11:47
  • But you are still not checking the second value for `$row_WaterTemp['ColdMax'])` as OP does in his code. – simon Sep 07 '16 at 11:59
  • @simon Finally got it... Thank you. – pedrouan Sep 07 '16 at 12:14
0

You can use break statement in this case when the flag is set to 1. As per your specification the Cold water should be less than 21, I have modified the code.

<?php

$row_WaterTemp['HotMin'] = "50";
$row_WaterTemp['Hotmax'] = "66";
$row_WaterTemp['ColdMax'] = "21";

$SeqWaterArray = array("58", "21");


foreach($SeqWaterArray as $key => $val) {
    $fields[] = "$key = '$val'";
    if($key == 0) { 
        if($val < $row_WaterTemp['HotMin'] || $val > $row_WaterTemp['Hotmax']) {
            $Status = 1;
            $WaterHot = $val;
            break;
        } else {
            $Status = 0;
            $WaterHot = $val;
        }

    }
if($key == 1) {
    if($val >= $row_WaterTemp['ColdMax']) {
        $Status = 1;
        $WaterCold = $val;
        break;
    } else {
        $Status = 0;
        $WaterCold = $val;
    }
}   

}
echo $Status;

?>

This way it would be easier to break the loop in case if the temperature fails to fall within the range in either case.

https://eval.in/636912

SanketR
  • 1,182
  • 14
  • 35
0

I don't think that you need a foreach loop here since you are working with a simple array and apparently you know that the first element is the hot water temperature and the second element is the cold water temperature. I would just do something like this:

$row_WaterTemp['HotMin'] = 50;
$row_WaterTemp['HotMax'] = 66;
$row_WaterTemp['ColdMax'] = 21;

$SeqWaterArray = array(58, 21);

$waterHot = $SeqWaterArray[0];
$waterCold = $SeqWaterArray[1];

$status = 0;

if ($waterHot < $row_WaterTemp['HotMin'] || $waterHot > $row_WaterTemp['HotMax']) {
    $status = 1;
}
if ($waterCold > $row_WaterTemp['ColdMax']) {
    $status = 1;
}

You can combine the if statements of course. I separated them because of readability.

Note that I removed all quotes from the numbers. Quotes are for strings, not for numbers.

simon
  • 2,896
  • 1
  • 17
  • 22