List array within exceptions (IF) and generate new status array

Asked

Viewed 33 times

0

I created an algorithm that checks an array of percentages and returns a status according to the value, but my array STATUS is returning me only the last value. I would like the status array to receive all values within it, for example $percentinstalled[0] = 50, then $status[0] = 'critical', and so on...

<?php

$percentinstalled = array(
    0=>'50.0',
    1=>'93.1',
    2=>'100.0',
    3=>'75.2',
    4=>'69.0',
    5=>'100.0',
    6=>'96.7',
    7=>'38.7',
    8=>'22.7',
    9=>'91.6',
    10=>'33.3',
    11=>'100.0',
    12=>'96.9',
    13=>'54.3',
    14=>'67.2',
    15=>'81.9'
);

$status = array();

for ($j = 0; $j <= 15; $j++) {
    if ($percentinstalled[$j] >= 98.0) {
        $status = array(
            $j => "ok",
        );
    } elseif ($percentinstalled[$j] >= 95.0) {
        $status = array(
            $j => "warning",
        );
    } elseif ($percentinstalled[$j] >= 80.0) {
        $status = array(
            $j => "alert",
        );
    } elseif ($percentinstalled[$j] <= 79.9) {
        $status = array(
            $j => "critical",
        );
    } else {
        $status = array(
            $j => "Unknow",
        );
    }

}

foreach ($status as &$val3) {
    echo $val3 . "</br>";
}

?>

VIEW CODE

GOAL:

I have the array $percentinstalled with all values in float, I would like to pass all these values within one IF where according to these values he returns to me what was defined within this exception, that our case would be a string where the value can be: (ok for >= 98.0, Warning for >= 95.0, Alert for >= 80.0, Critical for <= 79.9 or unknow if no value is found). In the current form it is only saving the last array value (position 15), and not all values as desired.

  • I don’t quite understand what you want to do, can you explain it better? want to pass a couple of values to the array?

  • I have the array $percentinstalled with all values in float, I would like to pass all these values within one IF where according to this value he returns to me what was set within this exception, that our case would be a string with the value of: (ok, Warning, Alert, Critical or unknow). In the current form it is only saving the last value of the array (position 15), and not all values as desired.

  • 1

    Exchange the $status = array($j => "") for $status[] = array($j => "texto"). So you will concatenate the values. Or, simply: $status[$j] = "texto"

  • 1

    It worked out, buddy, thanks!

2 answers

3

The operation you need to do is mapping. Convert an input value into an output value and as its name says, you can use the function array_map.

Create a conversion function:

function setStatusByPercentValue($value) {
    if ($value >= 98.0) return 'ok';
    if ($value >= 95.0) return 'warning';
    if ($value >= 80.0) return 'alert';
    return 'critical';
}

And then do:

$status = array_map('setStatusByPercentValue', $percentinstalled);

For the input given in the question the output would be:

Array
(
    [0] => critical
    [1] => alert
    [2] => ok
    [3] => critical
    [4] => critical
    [5] => ok
    [6] => warning
    [7] => critical
    [8] => critical
    [9] => alert
    [10] => critical
    [11] => ok
    [12] => warning
    [13] => critical
    [14] => critical
    [15] => alert
)

2


If I understand correctly, what you want to do is simple, just you assign the status setting the position of array.

$status = [];
// Usei o count para pegar o tamanho total do array e não precisar "setar" diretamente
for ($j = 0; $j <= count($percentinstalled); $j++) {  
    if ($percentinstalled[$j] >= 98.0) {
        $status[$j] =  "ok";
    } elseif ($percentinstalled[$j] >= 95.0) {
        $status[$j] = "warning";
    } elseif ($percentinstalled[$j] >= 80.0) {
        $status[$j] = "alert";
    } elseif ($percentinstalled[$j] <= 79.9) {
        $status[$j] = "critical";
    } else {
        $status[$j] = "Unknow";
    }
}

Thus the array status will stay like this:

Array
(
    [0] => critical
    [1] => alert
    [2] => ok
    [3] => critical
    [4] => critical
    [5] => ok
    [] => warning
    [7] => critical
    [8] => critical
    [9] => alert
    [10] => critical
    [11] => ok
    [13] => critical
    [14] => critical
    [15] => alert
)
  • 1

    opa, thank you very much, it worked here :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.